1. 按文件类型判断
示例:
格式1
[root@localhost tiaojianpanduan]# ls
mytest.log
[root@localhost tiaojianpanduan]# test -e mytest.log
格式2
[root@localhost tiaojianpanduan]# [ -e /home/shell/tiaojianpanduan/mytest.log ]
[root@localhost tiaojianpanduan]# echo $?
0
[root@localhost tiaojianpanduan]# [ -e /home/shell/tiaojianpanduan/mytest.log ] && echo yes || echo no
yes
2. 按文件权限判断
示例
[root@localhost tiaojianpanduan]# [ -r /home/shell/tiaojianpanduan/mytest.log ] && echo yes || echo no
yes
[root@localhost tiaojianpanduan]# [ -w /home/shell/tiaojianpanduan/mytest.log ] && echo yes || echo no
yes
[root@localhost tiaojianpanduan]# [ -g /home/shell/tiaojianpanduan/mytest.log ] && echo yes || echo no
no
3. 两个文件之间的比较
示例
[root@localhost tiaojianpanduan]# ln mytest.log newtest.log
[root@localhost tiaojianpanduan]# [ mytest.log -ef newtest.log ] && echo yes || echo no
yes
[root@localhost tiaojianpanduan]# [ mytest.log -nt mytest1.log ] && echo yes || echo no
no
[root@localhost tiaojianpanduan]# [ mytest.log -ot mytest1.log ] && echo yes || echo no
yes
4. 两个整数之间的比较
示例
[root@localhost tiaojianpanduan]# [ 1 -eq 2 ] && echo yes || echo no
no
[root@localhost tiaojianpanduan]# [ 1 -eq 1 ] && echo yes || echo no
yes
5. 字符串判断
示例
[root@localhost tiaojianpanduan]# [ -z "" ] && echo yes || echo no
yes
[root@localhost tiaojianpanduan]# [ -z "123" ] && echo yes || echo no
no
[root@localhost tiaojianpanduan]# [ "abc" == "abc" ] && echo yes || echo no
yes
[root@localhost tiaojianpanduan]# [ "abc" == "def" ] && echo yes || echo no
no
6. 多重条件判断
[root@localhost tiaojianpanduan]# [ ! -z "abc" ] && echo yes || echo no
yes
-z "abc"返回no,取反后返回yes