条件表达式,我们非常的常用,可以说,任何编程语言,都离不开条件表达式,但是每种变成语言的写法都不太一样,在shell中,有一种独特的写法。

条件表达式

条件测试语句,我们又叫做test语句

格式 格式
test <条件> 常规判断命令
[[ <条件> ]] 支持运算符和正则的条件表达式
[ <条件> ] 常用条件表达式

文件表达式

表达式 含义 举例
-d 判断目录是否存在 test -d /etc
-f 判断文件是否存在 test -f /etc/hosts
-e 判断是否存在 test -e /etc
-r 判断文件是否存在,并且有read权限 test -r /etc/hosts
-w 判断文件是否存在,并且有write权限 test -w /etc
-x 判断文件是否存在,并且有execute权限 test -x /bin/ls
-s 判断文件是否存在,并且size是否大于0 test -s /etc/passwd
-L 判断文件是否存在,并且是否是软链接文件 test -L /bin
file -nt file2 file1是否比file2更新,newer than test 1.txt -nt 2.txt
file -ot file2 file1是否比file2更旧,older than test 1.txt -ot 2.txt
# 1.判断目录是否存在
[root@zabbix01 ~]# test -d /etc && echo '存在' || '不存在'
存在
[root@zabbix01 ~]# [ -d /etc ] && echo '存在' || '不存在'
存在
[root@zabbix01 ~]# [[ -d /etc ]] && echo '存在' || '不存在'
存在

# 2.判断文件是否存在
[root@zabbix01 ~]# [[ -f /etc/hosts ]] && echo '存在' || echo '不存在'
存在
[root@zabbix01 ~]# [ -f /etc/hosts ] && echo '存在' || echo '不存在'
存在
[root@zabbix01 ~]# test -f /etc/hosts && echo '存在' || echo '不存在'
存在

# 3.判断是否存在,不区分目录或文件
[root@zabbix01 ~]# test -e /etc/hosts && echo '存在' || echo '不存在'
存在
[root@zabbix01 ~]# [ -e /etc/hosts ] && echo '存在' || echo '不存在'
存在
[root@zabbix01 ~]# [[ -e /etc/hosts ]] && echo '存在' || echo '不存在'
存在

# 4.判断权限,注意如果是root用户,只能判断出来执行权限,使用普通用户即可
[root@zabbix01 ~]# ll /etc/shadow
---------- 1 root root 791 Aug 17 19:57 /etc/shadow
[root@zabbix01 ~]# [ -x /etc/shadow ] && echo '存在' || echo '不存在'
不存在
[root@zabbix01 ~]# [ -r /etc/shadow ] && echo '存在' || echo '不存在'
存在
[root@zabbix01 ~]# [ -w /etc/shadow ] && echo '存在' || echo '不存在'
存在

# 5.判断文件是否为空
[root@zabbix01 ~]# echo 123 >1.txt
[root@zabbix01 ~]# touch 2.txt
[root@zabbix01 ~]# [ -s 1.txt ] && echo '不为空' || echo '空文件'
不为空
[root@zabbix01 ~]# [ -s 2.txt ] && echo '不为空' || echo '空文件'
空文件

# 6.判断文件是否是软链接文件
[root@zabbix01 ~]# [ -L /bin ] && echo '软链接' || echo '非软链接'
软链接

# 7.判断文件新或旧 (修改时间)
[root@zabbix01 ~]# [ 1.txt -nt 2.txt ] && echo '1.txt比2.txt更新' || echo '2.txt比1.txt更新'
2.txt比1.txt更新
[root@zabbix01 ~]# [ 1.txt -ot 2.txt ] && echo '1.txt比2.txt更旧' || echo '2.txt比1.txt更旧'
1.txt比2.txt更旧

## 条件表达式使用格式
[ 条件 ] && cmd

[ 条件 ] && {
cmd1
cmd2
}

字符串表达式

介绍

注意:大写的注意,在shell脚本中所有变量的调用,请一定加上双引号,尤其是和字符串相关的,

-n 判断字符串是否为空
test -n :与-z相反,字符串为空时假,不为空时为真
[root@web02 test]# test -n "$abc"
[root@web02 test]# echo $?
1
[root@web02 test]# abc=123
[root@web02 test]# test -n "$abc"
[root@web02 test]# echo $?
0
test -Z :判断字符串是否为空,空为真,反之为假
[root@web02 test]# test -z "$abcd" && echo '字符串为空' || echo '字符串不为空'
字符串不为空
"str1" = "str2"
[root@web02 test]# test 'abc' = 'abc' && echo '字符相等' || echo '字符不等'
字符相等
[root@web02 test]# test 'abc' = 'abz' && echo '字符相等' || echo '字符不等'
字符不等
"str1" != "str2"
[root@web02 test]# test 'abc' != 'abz' && echo '字符不等' || echo '字符相等'
字符不等
[root@web02 test]# test 'abc' != 'abc' && echo '字符不等' || echo '字符相等'
字符相等

整数表达式

表达式 含义 test举例 [[]]举例
-eq equal 等于 test 2 -eq 2 [[ 2 -eq 2 ]]
-ne not equal 不等于 test 2 -ne 2 [[ 2 != 2 ]]
-gt great than 大于 test 2 -gt 2 [[ 2 > 2 ]]
-lt less than 小于 test 2 -lt 2 [[ 2 < 2 ]]
-ge great equal 大于等于 test 2 -ge 2 [[ 2 >= 2 ]]
-le less equal 小于等于 test 2 -le 2 [[ 2 <= 2 ]]
[root@web02 test]# [ 2 > 1 ] && echo '成功' || echo '失败'
成功
[root@web02 test]# [[ 2 >= 1 ]] && echo '成功' || echo '失败'
bash: syntax error in conditional expression
bash: syntax error near `1'
# 单括号有时候会把>识别为重定向
[root@web02 test]# [ $age > 20 ] && echo '成功' || echo '失败'
失败
[root@web02 test]# age=20
[root@web02 test]# [ $age \> 10 ] && echo '成功' || echo '失败'   //使用\转义符
成功
[root@web02 test]# ll
total 8
-rw-r--r-- 1 root root 0 Jun 28 11:22 1
-rw-r--r-- 1 root root 0 Jun 28 11:25 20
-rw-r--r-- 1 root root 670 Jun 28 11:00 jsq.sh
-rw-r--r-- 1 root root 487 Jun 28 10:22 mxjsq.sh
[root@web02 test]# [ $age \> 10 ] && echo '成功' || echo '失败'
成功
[root@web02 test]# [ $age \> 3 ] && echo '成功' || echo '失败'
失败
[root@web02 test]# [ $age \> 10 ] && echo '成功' || echo '失败'
成功
[root@web02 test]# [ $age -gt 10 ] && echo '成功' || echo '失败'
失败

[[]]的成员运算

=~:成员运算
# 前面的值包不包含后面你的输入
[root@web02 test]# name=dongdong
[root@web02 test]# [[ $name =~ 'd' ]] && echo '包含' || echo '不包含'
包含
[root@web02 test]# [[ $name =~ 'i' ]] && echo '包含' || echo '不包含'
不包含
[root@web02 test]# [[ $name =~ 'do' ]] && echo '包含' || echo '不包含'
包含
[root@web02 test]# [[ $name =~ [0-9] ]] && echo '包含' || echo '不包含'
不包含
[root@web02 test]# [[ $name =~ [a-z] ]] && echo '包含' || echo '不包含'
包含
[root@web02 test]# [[ $name =~ ^[a-z] ]] && echo '包含' || echo '不包含'
包含
[root@web02 test]# [[ $name =~ [a-z]$ ]] && echo '包含' || echo '不包含'
包含
[root@web02 test]# [[ $name =~ ^[a-z]$ ]] && echo '包含' || echo '不包含'
不包含
[root@web02 test]# [[ $name =~ ^[a-z]+$ ]] && echo '包含' || echo '不包含'
包含
[root@web02 test]# [[ $name =~ 'g'$ ]] && echo '包含' || echo '不包含'
包含
[root@web02 test]# [[ $name =~ 'd'$ ]] && echo '包含' || echo '不包含'
不包含

逻辑运算表达式

逻辑运算表达式介绍

表方式 含义 符号
! 非,取反
&& -a
|| -o

and条件

只有都为真,条件才为真,条件中有任何一个为假,那么结果就是假

逻辑符号            条件一                     条件二                     结果
                    真                         真                         真
 -a                 真                         假                         假
                      假                         真                         假
                      假                         假                         假

-or条件

只要有一个为真,结果就是真,除非两个都为假,结果才是假。

逻辑符号            条件一                     条件二                     结果
                    真                         真                         真
 -o                 真                         假                         真
                      假                         真                         真
                      假                         假                         假