条件测试

1、test<测试表达式>

使用test语句测试 file这个文件是否存在,如果存在则echo true否则则echo false
[root@zhangjie scripts]# test -f file && echo true || echo false
false
[root@zhangjie scripts]# touch file
[root@zhangjie scripts]# test -f file && echo true || echo false
true
[root@zhangjie scripts]# 

取反操作,如果不存在则echo true,如果存在则echo false
[root@zhangjie scripts]# ls file
file
[root@zhangjie scripts]# test ! -f file && echo true || echo false
false

2、使用[<测试表达式>] 的方式

 使用 [<测试表达式>] 测试 file这个文件是否存在,如果存在则echo true否则则echo false
[root@zhangjie scripts]# ls file 
file
[root@zhangjie scripts]# [ -f file ] && echo true ||echo false  

 取反操作使用 [<测试表达式>] 测试 file这个文件是否不存在,如果不存在则echo true如果存在则echo false
true
[root@zhangjie scripts]# [ ! -f file ] && echo true ||echo false
false

3、使用[[<测试表达式>]] 的方式

[root@zhangjie scripts]# touch file01 file02
[root@zhangjie scripts]# ls file*
file01  file02
[root@zhangjie scripts]# [[ -f file01 && file02 ]] && echo true || echo false
true
[root@zhangjie scripts]# rm -f file02 
[root@zhangjie scripts]# [[ -f file01 || ! -f file02 ]] && echo true || echo false               
true
[root@zhangjie scripts]# [[ ! -f file02 ]] && echo true || echo false             
true
[root@zhangjie scripts]# 

说明:[[]]这种双中括号的写法支持 && 与 || 的写法,而单中括号则不支持这种写法,只支持-a 与-o的写法
在单中括号里面-a 与双中括号中的&& 是一个意思 (-a表示and的意思)  单中括号中的-o与双中括号中的 || 是一个意思(-o 表示or的意思)

4、文件测试操作符
常用文件测试操作符:
-f 文件   若文件存在且为普通文件时为真
-d 目录  若文件存在并且是目录文件时为真
-s文件    若文件存在且不为空时为真
-e 文件   若文件存在时为真
-r 文件    若文件存在且可读则为真
-w文件   若文件存在且可写则为真
-x 文件   若文件存在且可执行则为真
-L文件    若文件存在且是链接文件则为真
f1 -nt f2 若文件f1比文件f2新时为真
f1 -ot f2 若文件f2比文件f2 旧时为真

5、字符串测试操作符
-z "字符串"   若长度为0则为真
-n "字符串"   若长度不为0则为真
“字符串1” = “字符串2”  若串1 等于串2则为真  可以使用“==”代替 “=”

6、整数二元比较操作符

在[] 中使用的比较符                    在(())和[[]] 中使用的比较符                    说明
-eq                                               ==                                                        equal         等于
-ne                                               !=                                                         not equal  不等于
-gt                                                >                                                          great than  大于
-ge                                               >=                                                        great equal   大于等于
-lt                                                 <                                                          less than  小于
-le                                                <=                                                        less equal  小于等于

实例:
单中括号里>< 号要转义
[root@zhangjie scripts]# [ 2 \> 1 ] && echo true || echo false
true
[root@zhangjie scripts]# [ 2 \< 1 ] && echo true || echo false 
false
[root@zhangjie scripts]# [ 2 = 1 ] && echo true || echo false  
false
[root@zhangjie scripts]# [ 1 = 1 ] && echo true || echo false 
true
[root@zhangjie scripts]# [ 2 != 1 ] && echo true || echo false 

在双中括号里><号不用转义
[root@zhangjie scripts]# [[ 2 > 1 ]] && echo true || echo false     
true
[root@zhangjie scripts]# [[ 2 < 1 ]] && echo true || echo false 
false
[root@zhangjie scripts]# [[ 2 = 1 ]] && echo true || echo false 
false

整个比较使用单中括号时尽量使用二元比较符
[root@zhangjie scripts]# [ 2 -gt 1 ] && echo true || echo false 
true
[root@zhangjie scripts]# [ 2 -lt 1 ] && echo true || echo false  
false
[root@zhangjie scripts]# [ 2 -eq 1 ] && echo true || echo false    
false
[root@zhangjie scripts]# [ 2 -eq 2 ] && echo true || echo false   
true
[root@zhangjie scripts]# [ 2 -ne 1 ] && echo true || echo false    
true
[root@zhangjie scripts]# [ 2 -ge 1 ] && echo true || echo false   
true
[root@zhangjie scripts]# [ 2 -le 1 ] && echo true || echo false   
false

7、逻辑操作符
在[]中使用的逻辑操作符                        在[[]]中使用的逻辑操作符                       说明
-a                                                          &&                                                         与  两边都为真,则为真
-o                                                          ||                                                             或  两边有一边为真,则为真
!                                                          !                                                            非   相反则为真

说明:-a 是and 的意思  -o 是or的意思