Shell脚本的条件测试与比较
test
-f :表示文件存在且为普通文件则表达式成立
[[email protected] /]# test -f file && echo true || echo false
false
[[email protected] /]# touch file
[[email protected] /]# test -f file && echo true || echo false
true
-z:测试字符串的长度为0,则表达式成立
在shell中1为真,0为假
常用文件测试操作符
-d directory 文件存在且为目录则为真
-f file 文件存在且为普通文件则为真
-e exsit 文件存在则为真(不用区别为目录还是文件)
-r read 文件存在且可读则为真
-s size 文件存在且文件大小不为0则为真
-w write 文件存在且可写则为真
-x executable 文件存在且可执行则为真
-L link 文件存在且为链接文件则为真
f1 -nt f2 newer than 文件f1比文件f2新则为真
f1 -ot f2 older than 文件f1比文件f2旧则为真
[[email protected] /]# [ -f file ] && echo 1
1
字符串测试表达式
-n no zero 字符串的长度不为0,则为真
-z zero 字符串的长度为0 则为真
"串1" = "串2" 字符串1等于字符串2则为真
"串1" != "串2" 字符串1不等于字符串2则为真
整数二元比较操作符
-eq equal 等于
-ne not equal 不等于
-gt greater than 大于
-ge greater equal 大于等于
-lt less than 小于
-le less equal 小于等于
逻辑操作符
-a && and
-o || or
! 非