#############文件判断
test -e /root/file && echo "exist" || echo "not exsit"
test -f /root/file && echo "exist" || echo "not exsit" #是否存在这个file
test -d /root/file && echo "exist" || echo "not exsit" #是否存在这个目录
############文件的权限检测
test -r /root/file && echo "read" || echo "not read"
test -w /root/file && echo "write" || echo "not write"
test -x /root/file && echo "exeute" || echo "not exeute"
###########两个文件之间比较
-rw-r--r-- 1 root root 1105 Nov 11 01:39 passwd1
-rw-r--r-- 1 root root 1120 Nov 11 01:41 passwd2
test passwd1 -nt passwd2 && echo "new" || echo "not new"
not new
test passwd1 -ot passwd2 && echo "old" || echo "not old"
old
test passwd1 ef passwd2 && echo "old" || echo "not old"
passwd1和passwd2是否是用一个文件-》指向同一个Inode
###########两个数字的比较
[root@IPv6 gaole_scripts]# test 1 -eq 2 && echo "1" || echo "2"
2
[root@IPv6 gaole_scripts]# test 1 -ne 2 && echo "1" || echo "2"
1
[root@IPv6 gaole_scripts]# test 1 -ge 2 && echo "1" || echo "2"
2
[root@IPv6 gaole_scripts]# test 1 -le 2 && echo "1" || echo "2"
1
[root@IPv6 gaole_scripts]# test 1 -gt 2 && echo "1" || echo "2"
2
[root@IPv6 gaole_scripts]# test 1 -lt 2 && echo "1" || echo "2"
##############字符串判定
test="123456"
[root@IPv6 gaole_scripts]# test -n $test && echo "empty" || echo "not empty"
empty #判断字符串为空
[root@IPv6 gaole_scripts]# test -n ${test}="test" && echo "empty" || echo "not empty"
empty #判断字符串为非空
[root@IPv6 gaole_scripts]# test ${test} = "test" && echo "empty" || echo "not empty"
not empty #判断字符串相等,等号两边要有空格
[root@IPv6 gaole_scripts]# test ${test} != "test" && echo "empty" || echo "not empty"
empty #判断字符串相等,!等号两边要有空格
[root@IPv6 gaole_scripts]#
###########多重条件判断
[root@IPv6 gaole_scripts]# test -r error.txt -a -w error.txt && echo "empty" || echo "not empty"
empty #可读并且可写,-a and
[root@IPv6 gaole_scripts]# test -r error.txt -a -x error.txt && echo "empty" || echo "not empty"
not empty #可读并且可执行
[root@IPv6 gaole_scripts]# test -r error.txt -o -x error.txt && echo "empty" || echo "not empty"
not empty #可读或者可执行,-o or
[root@IPv6 gaole_scripts]# test ! -r error.txt -o -x error.txt && echo "empty" || echo "not empty"
not empty # ! 取反
#############[]中括号里的注意事项
1.【】两边都要有空格,中间的符号和变量直接也要有空格
2.字符串的话,变量和常量最好都加上双引号
3.字符串比较如果有通配符的话,要用两个中括号生效通配符
4.以上的test可以换成【 -z string】类型
比如
[root@IPv6 gaole_scripts]# test -r error.txt -a -w error.txt && echo "empty" || echo "not empty"
empty
[root@IPv6 gaole_scripts]# [ -r error.txt -a -w error.txt ] && echo "empty" || echo "not empty"
empty
[root@IPv6 gaole_scripts]# [ -r error.txt ] && [ -w error.txt ] && echo "empty" || echo "not empty"
[root@IPv6 gaole_scripts]# [ -r error.txt ] || [ -w error.txt ] && echo "empty" || echo "not empty"
934

被折叠的 条评论
为什么被折叠?



