shell的条件测试

1)文件测试

[ -e file|dir ]     存在即为真
[ -f file     ]     文件存在即为真
[ -d dir      ]     目录存在即为真
[  -r file    ]     是否有读的权限
[  -w file    ]     是否有写入权限
[  -x file    ]     是否有执行权限
[root@shell ~]# [ -f /etc/hosts ] && echo "文件存在"
文件存在
[root@shell ~]# [ -f /etc/hostss ] && echo "文件存在" || echo "文件不存在"
文件不存在  
[root@shell ~]# [ -d /etc ] && echo "文件存在" || echo "文件不存在" 
文件存在  
[root@shell ~]# chmod +x 1.sh
[root@shell ~]# ll 1.sh
-rwxr-xr-x 1 root root 10 Feb 28 19:29 1.sh
[root@shell ~]# [ -x 1.sh ] && echo "文件存在" || echo "文件不存在" 
文件存在  
[root@shell ~]# [ -r 1.sh ] && echo "文件存在" || echo "文件不存在"  
文件存在
[root@shell ~]# [ -w 1.sh ] && echo "文件存在" || echo "文件不存在"  
文件存在
[root@shell ~]# [ -L 1.sh ] && echo "文件存在" || echo "文件不存在"  
文件不存在
[root@shell ~]# [ -L /etc/rc.local ] && echo "文件存在" || echo "文件不存在"  
取反        
[root@shell ~]# [ ! -f /etc/hosts ] && echo ok || echo error
error

案例: -f

[root@shell scripts]# cat test.sh 
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
read -p "Please input url: " url
ping -c1 -W1 $url &>/dev/null
[ $? -eq 0 ] && action "ping $url is" /bin/true || action "ping $url" /bin/false

案例:-d

[root@shell ~]# [ -d /backup ] || mkdir /backup
[root@shell ~]# dir=/etc/;[ -d $dir ] && tar zcf etc.tar.gz $dir || echo "目录不存在"
  1. 整数数值比较
[ 整数1 比较符 整数2 ]
[ 10 -eq 10 ]  等于
[ 10 -gt 5  ]  大于
[ 10 -ge 10 ]  大于或等于
[ 10 -lt 20 ]  小于
[ 10 -le 10 ]  小于或等于
[ 10 -ne 20 ]  不等于

注意 如果是在[[]] (()) 中的比较符 = != > >= < <=

[root@shell ~]# [ 10 -eq 10 ] && action /bin/true
/bin/true                                                  [  OK  ]
[root@shell ~]# [ 10 -eq 10 ] && action /bin/true || action /bin/false
/bin/true                                                  [  OK  ]
[root@shell ~]# [ 10 -ne 10 ] && action /bin/true || action /bin/false  
/bin/false                                                 [  OK  ]
[root@shell ~]# [ 10 -gt 10 ] && action /bin/true || action /bin/false  
/bin/false                                                 [  OK  ]
[root@shell ~]# [ 10 -lt 10 ] && action /bin/true || action /bin/false 
/bin/false 

案例:
查看磁盘使用率,如果使用率超过5% 则报警发邮件 否则提示正常 并且输出当前使用率

[root@shell day2]# cat disk.sh 
#!/bin/sh
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
usedisk=$(df -h|grep '/$'|awk '{print $5}')
usedisk=`df -h|grep '/$'|awk '{print $5}'`
[ ${usedisk%\%} -gt 80 ] && action /bin/false || action /bin/true

案例2:内存百分比

[root@shell day2]# cat free.sh 
#!/bin/bash
Mem_Use=$(free|awk 'NR==2{print $3/$2*100}')
[ ${Mem_Use%.*} -gt 50 ] && echo mail|| echo ok
  1. 多整数比较
    -a and 两端都要成立则为真
    -o or 一端成立则为真
    [ 整数1 比较符 整数2 -a 整数3 比较符 整数4 ]
[root@shell ~]# [ 10 -eq 10 -a 20 -gt 10 ]
[root@shell ~]# [ 10 -eq 10 -a 20 -gt 10 ] && action /bin/true
/bin/true                                                  [  OK  ]
[root@shell ~]# [ 10 -eq 10 -a 20 -gt 10 ] && action /bin/true || action /bin/false
/bin/true                                                  [  OK  ]
[root@shell ~]# [ 10 -ne 10 -a 20 -gt 10 ] && action /bin/true || action /bin/false  
/bin/false                                                 [  OK  ]

[root@shell ~]# [ 10 -ne 10 -o 20 -gt 10 ] && action /bin/true || action /bin/false 
/bin/true                                                  [  OK  ]

[[]] && ||
[root@shell ~]# [[ 10 -ne 10 || 20 -gt 10 ]] && action /bin/true || action /bin/false 
/bin/true                                                  [  OK  ]
[root@shell ~]# [[ 10 -ne 10 && 20 -gt 10 ]] && action /bin/true || action /bin/false 
/bin/false                                                 [  OK  ]
  1. 字符串比对 注意 字符串加双引号
    [ "字符串1" = "字符串2" ] 等于
    [ "字符串1" != "字符串2" ] 等于
[root@shell ~]# [ "oldboy" = "root" ]
[root@shell ~]# echo $?
1
[root@shell ~]# [ "oldboy" = "root" ] && echo 成立 || echo 不成立
不成立
[root@shell ~]# 
[root@shell ~]# [ "oldboy" = "oldboy" ] && echo 成立 || echo 不成立    
成立
[root@shell ~]# [ "oldboy" != "root" ] && echo 成立 || echo 不成立  
成立
    
[root@shell ~]# [ "$USER" = "root" ] && echo 成立 || echo 不成立
成立
[root@shell ~]# [ "$USER" = "oldboy" ] && echo 成立 || echo 不成立    
不成立
[root@shell ~]# [ "$USER" != "oldboy" ] && echo 成立 || echo 不成立
成立

-z 字符串长度为0 则为真
-n 字符串长度不为0 则为真

[root@shell ~]# oldboy=""
[root@shell ~]# echo $oldboy

[root@shell ~]# 
[root@shell ~]# [ -z "$oldboy" ] && echo ok || echo error
ok
[root@shell ~]# oldboy="test"
[root@shell ~]# echo $oldboy
test
[root@shell ~]# 
[root@shell ~]# [ -z "$oldboy" ] && echo ok || echo error
error
[root@shell ~]# [ -n "$oldboy" ] && echo ok || echo error 
ok

案例: 判断脚本的字符串传参

[root@shell ~]# cat test1.sh
#!/bin/sh
read -p "Please input you name: " name
[  -z $name ] && exit
echo $name

5)正则比对
[[ "字符串" 正则匹配 "字符串" ]]

[root@shell ~]# [[ "root" =~ ^r ]]
[root@shell ~]# 
[root@shell ~]# [[ "root" =~ ^r ]] && echo 成立|| echo 不成立
成立
[root@shell ~]# 
[root@shell ~]# [[ "root" =~ ^o ]] && echo 成立|| echo 不成立 
不成立
[root@shell ~]# [[ "root" =~ t$ ]] && echo 成立|| echo 不成立  
成立

[root@shell ~]# name=123
[root@shell ~]# [[ "$name" =~ ^[0-9]+$ ]]
[root@shell ~]# 
[root@shell ~]# [[ "$name" =~ ^[0-9]+$ ]] && echo 成立|| echo 不成立
成立
[root@shell ~]# 
[root@shell ~]# name=123q
[root@shell ~]# 
[root@shell ~]# [[ "$name" =~ ^[0-9]+$ ]] && echo 成立|| echo 不成立
不成立
[root@shell ~]# name=12,3
[root@shell ~]# 
[root@shell ~]# [[ "$name" =~ ^[0-9]+$ ]] && echo 成立|| echo 不成立
不成立

使用正则对传参的数字进行判断

[root@shell ~]# cat expr.sh 
#!/bin/sh
[ $# -ne 2 ] && echo "请输入两个整数" && exit
[[ ! "$1" =~ ^[0-9]+$ ]] && echo "请输入整数" && exit
[[ ! "$2" =~ ^[0-9]+$ ]] && echo "请输入整数" && exit
[ $1 -gt $2 ] && echo "$1 > $2"
[ $1 -lt $2 ] && echo "$1 < $2"
[ $1 -eq $2 ] && echo "$1 = $2"
[root@shell ~]# cat expr.sh 
#!/bin/sh
[ $# -ne 2 ] && echo "请输入两个整数" && exit
[[  "$1" =~ ^[0-9]+$ && "$2" =~ ^[0-9]+$ ]] || exit
#[[ ! "$2" =~ ^[0-9]+$ ]] && echo "请输入整数" && exit
[ $1 -gt $2 ] && echo "$1 > $2"
[ $1 -lt $2 ] && echo "$1 < $2"
[ $1 -eq $2 ] && echo "$1 = $2"
 
原文地址:https://www.cnblogs.com/youhongliang/p/12706330.html