Linux test命令:检查文件类型,并比较值。
Linux test命令 功能描述
使用test命令可以检查文件类型,并比较值。
Linux test命令 语法
test [表达式] [选项]
Bash
命令中各选项的含义如下表所示。
在使用test命令时,可以指定下表所示的表达式。
Linux test命令 示例
字符串比较
[root@rhel ~]# str1=abcd [root@rhel ~]# test str1=abcd [root@rhel ~]# echo? 0 //结果显示0表示字符串str1确实等于abcd
Bash
数字相等比较
[root@rhel ~]# int1=1234 [root@rhel ~]# int2=01234 [root@rhel ~]# test int1-eqint2 [root@rhel ~]# echo $? 0 //结果显示0表示字符int1和int2比较,二者值一样大
Bash
数字大于比较
[root@rhel ~]# int1=4 [root@rhel ~]# test int1-gt 2 [root@rhel ~]# echo? 0 //结果显示0表示字符int1的值确实大于2
Bash
逻辑测试
[root@rhel ~]# test -r empty -a -s empty [root@rhel ~]# echo $? 1 //结果显示1表示文件empty存在且只读及长度为0
Bash
文件操作测试
[root@rhel ~]# cat /dev/null>empty [root@rhel ~]# cat empty [root@rhel ~]# test -r empty [root@rhel ~]# echo ? 0 //结果显示0表示文件empty存在且只读 [root@rhel ~]# test ! -s empty [root@rhel ~]# echo? 0 //结果显示0表示文件empty存在且文件长度为0
Bash