shell脚本第一行总是以 #/bin/bash开始,这段脚本通知shell使用系统上的Bourne shell解释器。
任何脚本都可能有注释,加注释需要此行的第一个字符为 #,解释器对此行不予解释。
• 对文件、字符串和数字使用test命令。
• 对数字和字符串使用expr命令。
expr命令测试和执行数值输出。使用最后退出状态命令
1.test测试文件状态
test一般有两种格式,即:
test condition 文件名
或
[ condition 文件名 ]
使用方括号时,要注意在条件两边加上空格。
测试文件状态的条件表达式很多,但是最常用的可在下表中查到:
参数 | 含义 | 参数 | 含义 |
---|---|---|---|
-d | 目录 | -s | 文件长度大于0,非空 |
-f | 正规文件 | -w | 可写 |
-L | 链接文件 | -u | 文件有suid位设置 |
-r | 可读 | -x | 可执行 |
首先我们创建一个空文件test1
[root@localhost test]# touch test1
[root@localhost test]# ll
总用量 0
-rw-r--r--. 1 root root 0 9月 19 14:35 test1
[root@localhost test]#
我们现在测试下该文件的状态:
是否可读
第一种方式:
[root@localhost test]# test -r test1
[root@localhost test]# echo $?
0
第二种方式:
[root@localhost test]# [ -r test1 ]
[root@localhost test]# echo $?
0
两种状态均返回0!
是否可执行?
[root@localhost test]# [ -x test1 ]
[root@localhost test]# echo $?
1
给文件赋予可写的权限,并测试文件是否可执行?
[root@localhost test]# chmod u+x test1
[root@localhost test]# test -x test1
[root@localhost test]# echo $?
0
2.测试时使用逻辑操作符
测试文件状态是否为 O K,但是有时要比较两个文件状态。 s h e l l提供三种逻辑操作完成此
功能。
逻辑操作符 | 逻辑操作符具体的含义 |
---|---|
-a | 逻辑与,操作符两边均为真,结果为真,否则为假。 |
-o | 逻辑或,操作符两边一边为真,结果为真,否则为假。 |
! | 逻辑否,条件为假,结果为真。 |
下面比较两个文件:
[root@localhost test]# ll #列出两个文件详细信息
总用量 0
-rwxr--r--. 1 root root 0 9月 19 14:35 test1
-rw-r--r--. 1 root root 0 9月 19 15:48 test2
[root@localhost test]# test -r test1 -a -r test2 #两个文件是否均可读?
[root@localhost test]# echo $?
0 #0 代表两个文件均可读
————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# test -x test1 -a -x test2 #两个文件是否都可执行?
[root@localhost test]# echo $?
1 #1 代表两个文件并不是都可执行
————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# test -x test1 -o -x test2 #测试其中一个文件是否可执行?
[root@localhost test]# echo $?
0 #0 代表其中一个文件是可执行
————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# test ! -x test1 #测试test1是否没有可执行权限?
[root@localhost test]# echo $?
1 #猜测不成功,test1是有可执行权限的
————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# test ! -x test2 #测试test2是否没有可执行权限?
[root@localhost test]# echo $?
0 #猜测成功,test2是没有有可执行权限的
[root@localhost test]#
3.字符串测试
字符串测试是错误捕获很重要的一部分,特别在测试用户输入或比较变量时尤为重要。
字符串测试有5种格式:
- test “string”
- test string_operator “string”
- test “string” string_operator“ string”
- [ string_operator string ]
- [ string string_operator string ]
string_operator 运算符 | 具体含义 |
---|---|
= | 两个字符串相等 |
!= | 两个字符串不等 |
-z | 空串 |
- n | 非空串 |
举例:
[root@localhost test]# name="zss" #定义变量name,值为zss
[root@localhost test]# test -z $name #测试name是否为空串
[root@localhost test]# echo $?
1 #name不是空串
——————————————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# [ $name = "vi" ] #测试name值是否为vi(注意等号两边的空格)
[root@localhost test]# echo $?
1 #name的值不是vi
——————————————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# [ $name = "zss" ] #测试name值是否为zss(注意等号两边的空格)
[root@localhost test]# echo $?
0 #name是值是zss
[root@localhost test]# echo $name #输出name值
zss
——————————————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# name1="zss" #定义变量name1,值为zss
[root@localhost test]# name2="ss" #定义变量name2,值为ss
[root@localhost test]# test "$name1" = "$name" #变量name1和变量name是否相等
[root@localhost test]# echo $?
0 #变量name1和变量name相等
——————————————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# test "$name1" = "$name2" #变量name1和变量name2是否相等
[root@localhost test]# echo $?
1 #变量name1和变量name2不相等
——————————————————————————————————————————————————————————————————————————————————————————————
[root@localhost test]# test "$name1" != "$name2" #变量name1和变量name2是否不等
[root@localhost test]# echo $?
0 #变量name1和变量name2不等
4.测试数值
测试数值可以使用许多操作符,一般格式如下:
“number” numeric_operator “number”
或者
[ “number” numeric_operator “number” ]
numeric_operator可为:
numeric_operator运算符 | 运算符具体含义 |
---|---|
-eq | 数值相等 |
-ne | 数值不相等 |
-gt | 第一个数大于第二个数 |
-lt | 第一个数小于第二个数 |
-le | 第一个数小于等于第二个数 |
-ge | 第一个数大于等于第二个数 |
下面的例子返回结果都一样。均为测试两个数是否相等( 130是否等于130)。
[root@localhost test]# NUM=130 #定义变量NUM,值为130
[root@localhost test]# [ "$NUM" -eq "130" ] #变量NUM是否等于130
[root@localhost test]# echo $?
0
[root@localhost test]# [ "$NUM" -eq "100" ] #变量NUM是否等于100
[root@localhost test]# echo $?
1
[root@localhost test]# [ "$NUM" -gt "100" ] #变量NUM是否大于100
[root@localhost test]# echo $?
0
也可以测试两个整数变量:
[root@localhost test]# NUM2=110 #定义变量NUM2,值为110
[root@localhost test]# [ "$NUM" -eq "$NUM2" ] #变量NUM是否等于NUM2
[root@localhost test]# echo $?
1
[root@localhost test]# [ "$NUM" -gt "$NUM2" ] #变量NUM是否大于NUM2
[root@localhost test]# echo $?
0
可以不必将整数值放入变量,直接用数字比较即可,但要加引号:
[root@localhost test]# [ "100" -gt "110" ] #100是否大于110
[root@localhost test]# echo $?
1
[root@localhost test]# [ "100" -le "110" ] #100是否小于110
[root@localhost test]# echo $?
0
可以用逻辑操作符将两个测试表达式结合起来。仅需要用到一对方括号,而不能用两个,
否则将返回错误信息“too many arguments”。
[root@localhost test]# [ "100" -le "110" ] -a [ "100" -gt "90" ]
-bash: [: 参数太多
[root@localhost test]# [ "100" -le "110" -a "100" -gt "90" ] #100是否小于110且小于90?
[root@localhost test]# echo $?
0
5.expr用法
expr命令一般用于整数值,但也可用于字符串。一般格式为:
expr argument operator argument
expr也是一个手工命令行计数器。
[root@localhost test]# expr 10+10
10+10
[root@localhost test]# expr 10 + 10
20
[root@localhost test]# expr 1000 + 1000
2000
[root@localhost test]# expr 30 / 6
5
[root@localhost test]# expr 30 / 6 / 5
1
#使用乘号时,必须用反斜线屏蔽其特定含义。因为 s h e l l可能会误解显示星号的意义。
[root@localhost test]# expr 30 \* 3
90