shell条件测试通常都会用在for、while、until、if等控制流结构中,用于判断文件的相关性质或变量的相互关系。
条件测试用法:test <表达式>
结果:成立返回0,不成立返回非0
查看结果:echo $?
以下是几类常用的测试表达式:
1 文件状态测试
-b filename : 当filename 存在并且是块文件时返回真(返回0)
-c filename :
当filename 存在并且是字符文件时返回真
-d pathname : 当pathname 存在并且是一个目录时返回真
-e pathname
: 当由pathname 指定的文件或目录存在时返回真
-f filename : 当filename 存在并且是正规文件时返回真
-g
pathname : 当由pathname 指定的文件或目录存在并且设置了SGID 位时返回真
-h filename : 当filename
存在并且是符号链接文件时返回真 (或 -L filename)
-k pathname : 当由pathname
指定的文件或目录存在并且设置了"粘滞"位时返回真
-p filename : 当filename 存在并且是命名管道时返回真
-r pathname
: 当由pathname 指定的文件或目录存在并且可读时返回真
-s filename : 当filename 存在并且文件大小大于0
时返回真
-S filename : 当filename 存在并且是socket 时返回真
-t fd : 当fd
是与终端设备相关联的文件描述符时返回真
-u pathname : 当由pathname 指定的文件或目录存在并且设置了SUID 位时返回真
-w
pathname : 当由pathname 指定的文件或目录存在并且可写时返回真
-x pathname : 当由pathname
指定的文件或目录存在并且可执行时返回真
-O pathname : 当由pathname 存在并且被当前进程的有效用户id 的用户拥有时返回真(字母O
大写)
-G pathname : 当由pathname 存在并且属于当前进程的有效用户id 的用户的用户组时返回真
file1 -nt file2
: file1 比file2 新时返回真
file1 -ot file2 : file1 比file2 旧时返回真
举例: if [ -b
/dev/hda ] ;then echo "yes" ;else echo "no";fi // 结果将打印 yes;
test -c /dev/hda ; echo $? // 结果将打印 1 ;表示test 命令的返回值为1,因为/dev/hda
不是字符设备
[ -w /etc/passwd ]; echo $? // 查看对当前用户而言,passwd
文件是否可写
2 测试时使用逻辑操作符
-a 逻辑与,操作符两边均为真,结果为真,否则为假。
-o
逻辑或,操作符两边一边为真,结果为真,否则为假。
! 逻辑否,条件为假,结果为真。
举例: [ -w result.txt -a -w
score.txt ] ;echo $? // 测试两个文件是否均可写
3 常见字符串测试
-z string :
字符串string 为空串(长度为0)时返回真
-n string : 字符串string 为非空串时返回真
str1 = str2 :
字符串str1 和字符串str2 相等时返回真
str1 != str2 : 字符串str1 和字符串str2 不相等时返回真
str1 <
str2 : 按字典顺序排序,字符串str1 在字符串str2 之前
str1 > str2 : 按字典顺序排序,字符串str1 在字符串str2
之后
举例: name="zqf"; [ $name = "zqf" ];echo $? // 打印 0 ,表示变量name
的值和字符串"zqf"相等
4 常见数值测试
int1 -eq int2 : 如果int1 等于int2,则返回真
int1 -ne
int2 : 如果int1 不等于int2,则返回真
int1 -lt int2 : 如果int1 小于int2,则返回真
int1 -le
int2 : 如果int1 小于等于int2,则返回真
int1 -gt int2 : 如果int1 大于int2,则返回真
int1 -ge
int2 : 如果int1 大于等于int2,则返回真
举例: x=1 ; [ $x -eq 1 ] ; echo $? // 将打印 0
表示变量x 的值等于数字1
x=a ; [ $x -eq "1" ] // shell
打印错误信息 [: a: integer expression expected