Chinaunix首页 | 论坛 | 博客
  • 博客访问: 10493372
  • 博文数量: 2905
  • 博客积分: 20098
  • 博客等级: 上将
  • 技术积分: 36298
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-23 05:00
文章存档

2012年(1)

2011年(3)

2009年(2901)

分类: LINUX

2009-03-23 11:26:48

  因为以前学过点程序相关的东东,所以呢,这篇文章权当是参考罢了,很多细节方面的东东也不会像以前说得那么详细,就当自己能看懂就行了.当然,如果在此同时能给别人点帮助,那是最好不过了.有名话叫什么来的"自助而后天助",到头来能依靠的只能是自己.

变量:
  ("")双引号:用于标记多个特殊符号,但是对$,\,`,!符号例外
  ('')单引号:作用同上,但是可以标记""所不能的特殊符号
  (``)反引号[!前的那个符号,不是单引号]一般用来执行命令
  (\)反斜杠:转义符号,用于标记单个特殊符号.
[root@redhat ~]# winner=bob
[root@redhat ~]# notice="the person who won is $winner"
[root@redhat ~]# echo $notice
the person who won is bob ##将$winner解析成变量

[root@redhat ~]# winner=bob
[root@redhat ~]# notice='the person who won is $winner'
[root@redhat ~]# echo $notice
the person who won is $winner ##$winner只是符号
[root@redhat ~]# echo $winner
bob

[root@redhat ~]# winner=bob
[root@redhat ~]# notice="the person who won is \$winner"
[root@redhat ~]# echo $notice
the person who won is $winner ##用\将$转义成字符
[root@redhat ~]# echo $winner
bob

[root@redhat ~]# listnew='ls *.new'
[root@redhat ~]# echo $listnew
ls main.cf.new master.cf.new ##仔细查看,两个结果不一样的

[root@redhat ~]# listnew=`ls *.new`
[root@redhat ~]# echo $listnew
main.cf.new master.cf.new

字符:
  空格(space): 解析命令行参数
  (*)(?)({and}):产生文件名列表
  (.): 代表当前目录
  ($): 对变量求值
  (>)(<):    重定向标准输入或输出
  (&):       执行后台命令
  (|):       管道输出

一般命令:
  echo 显示变量及字符
  read 从用户处接受输入
  <<  输入重定向

脚本命令相关:
  $0 Linux命令名
  $n 命令行参数
  $* 由号码1开始的所有的命令行参数
  $@ 分别访问命令行参数
  $# 命令参数的个数

运算:
  exprort 将局部变量导出为该shell中的全局变量
  +,-,*,/,>,<,>=,<=,=(用于字符串的比较),==(用于数字的比较),!=,&(与),|(或),!(非).
[root@redhat ~]# let a=2*7
[root@redhat ~]# echo $a
14
[root@redhat ~]# let a="2 * 7"
[root@redhat ~]# echo $a
14
  test比较运算,在比较运算中,正确结果的返回值是0,而不是传统程序上的1
  -gt 一个数是否大于另一个数
  -lt 小于
  -ge 大于等于
  -le 小于等于
  -eq 等于(用于数值)
  -ne 不等于
  -z 是否为空字符串
  -n 字符串是否大于0
  =  字符串是否相等
  != 是否不等
  str 是否非空
  -a  与
  -o 或
  !  非
  -f 是否为普通文件
  -s 文件是否为空
  -r 文件是否可读
  -w 文件是否可写
  -x 文件是否可执行
  -d 是否为目录
  -h 是否为符号链接
  -c 是否与字符设备相关联
  -b 是否与块文件相关联
[root@redhat ~]# num=5
[root@redhat ~]# test $num -eq 10
[root@redhat ~]# echo $?
1 ##返回比较的结果值
[root@redhat ~]# num=5
[root@redhat ~]# test $num -eq 5
[root@redhat ~]# echo $?
0
下面的[]符号和test的作用等同
[root@redhat ~]# num=5
[root@redhat ~]# [ $num -eq 10 ] ##注意中间空格的地方
[root@redhat ~]# echo $?
1
[root@redhat ~]# num=5
[root@redhat ~]# [ $num -eq 5 ]
[root@redhat ~]# echo $?
0
给个小脚本试试
[root@redhat ~]# vi lss.sh
#!/bin/bash ##使用bash
echo This is a list Bash Script
echo S: list sizes
echo L: List all file information
echo C: list c Files
echo -n "Please enther choice:"
read choice ##等待用户输入
case $choice in
s|S)          ##如果输入的是s,不区分大小写
ls -s
;;
l|L)         ##如果输入的是l,不区分大小写
ls -l
;;
c|C) ##如果输入的是c,不区分大小写
ls *.new
;;
*) ##都不是的话
echo error
esac
[root@redhat ~]# chmod +x lss.sh
[root@redhat ~]# ./lss.sh
This is a list Bash Script
S: list sizes
L: List all file information
C: list c Files
Please enther choice:l
total 69
-rw-r--r-- 1 root root 1251 Oct 24 15:01 anaconda-ks.cfg
drwx------ 3 root root 1024 Oct 24 15:04 Desktop
-rw-r--r-- 1 root root 545 Jan 19 15:22 genhtml.sh
-rw-r--r-- 1 root root 47583 Oct 24 15:01 install.log
-rw-r--r-- 1 root root 4702 Oct 24 15:01 install.log.syslog
-rwxr-xr-x 1 root root 248 Jan 19 15:56 lss.sh
-rw-r--r-- 1 root root 2737 Jan 6 12:45 main.cf.new
-rw-r--r-- 1 root root 3125 Jan 6 12:45 master.cf.new
-rwxr-xr-x 1 root root 263 Dec 5 17:39 userandgroupadd.sh
嘿嘿,还比较好玩,比我想象中的要简单些.....

阅读(966) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~