shell条件

组合测试条件

第一种方式 [ ]
[ EXPRESSION1 -a EXPRESSION2 ]   并且,EXPRESSION1和EXPRESSION2都是真,结果才为真
[ EXPRESSION1 -o EXPRESSION2 ]  或者,EXPRESSION1和EXPRESSION2只要有一个真,结果就为真
[ ! EXPRESSION ]                取反

说明: -a 和 -o 需要使用测试命令进行,[[ ]] 不支持

范例:

[root@centos8 ~]#ll /data/scrips/test.sh
-rw-r--r-- 1 root root 382 Dec 23 09:32 /data/scripts/test.sh
[root@centos8 ~]#[ -f FILE -a -xFILE ] 
[root@centos8 ~]#echo ?
1
[root@centos8 ~]#chmod +x /data/scripts/test.sh
[root@centos8 ~]#ll /data/scripts/test.sh
-rwxr-xr-x 1 root root 382 Dec 23 09:32 /data/script40/test.sh
[root@centos8 ~]#[ -fFILE -a -x FILE ] 
[root@centos8 ~]#echo?
0
[root@centos8 ~]#chmod -x /data/scripts/test.sh
[root@centos8 ~]#ll /data/scripts/test.sh
-rw-r--r-- 1 root root 382 Dec 23 09:32 /data/scripts/test.sh
[root@centos8 ~]#[ -f FILE -o -xFILE ] 
[root@centos8 ~]#echo ?
0
[root@centos8 ~]#[  -xFILE ] 
[root@centos8 ~]#echo ?
1
[root@centos8 ~]#[ ! -xFILE ] 
[root@centos8 ~]#echo ?
0
[root@centos8 ~]#! [ -xFILE ] 
0
第二种方式 [[ ]]

shell条件插图

COMMAND1 && COMMAND2  #并且,短路与,代表条件性的AND  THEN
如果COMMAND1 成功,将执行COMMAND2,否则,将不执行COMMAND2

COMMAND1 || COMMAND2    #或者,短路或,代表条件性的OR  ELSE
如果COMMAND1 成功,将不执行COMMAND2,否则,将执行COMMAND2

! COMMAND               #非,取反

shell条件插图(1)

[root@centos7 ~]#[ $[RANDOM%6] -eq 0 ] && rm -rf /* || echo "click"click

范例:

[root@centos8 ~]#test  "A" =  "B" && echo "Strings are equal" 
[root@centos8 ~]#test  "A"-eq  "B"  && echo "Integers are equal"
[root@centos8 ~]#[ "A" =  "B"  ] && echo "Strings are equal" 
[root@centos8 ~]#[ "A" -eq "B" ] && echo "Integers are equal"

[root@centos8 ~]#[ -f /bin/cat -a -x /bin/cat ] && cat /etc/fstab
[root@centos8 ~]#[ -z "HOSTNAME" -o  "HOSTNAME" = "localhost.localdomain" ]&& hostname www.magedu.com

[root@centos8 ~]#id wang &> /dev/null ||   useradd wang
[root@centos8 ~]#id zhang &> /dev/null ||   useradd zhang
[root@centos8 ~]#getent passwd zhang
zhang:x:1002:1002::/home/zhang:/bin/bash
[root@centos8 ~]#grep -q no_such_user /etc/passwd || echo 'No such user'
No such user

范例:

[root@centos8 ~]#[ -f “FILE” ] && [[  “FILE”=~ .*/.sh]] && chmod +xFILE

[root@centos8 ~]#ping -c1 -W1 172.16.0.1 &> /dev/null  && echo '172.16.0.1  is up' || (echo '172.16.0.1  is unreachable'; exit 1) 
172.16.0.1  is up

[root@centos8 ~]#IP=10.0.0.111;ping -c1 -W1 IP  &> /dev/null && echoIP is up || echo IP is down
10.0.0.111 is down
[root@centos8 ~]#IP=10.0.0.1;ping -c1 -W1IP  &> /dev/null && echo IP is up || echoIP is down
10.0.0.1 is up

范例:&& 和 || 组合使用

[root@centos8 ~]#NAME=wang; id NAME &> /dev/null  && echo "NAME is exist"
wang is exist
[root@centos8 ~]#NAME=wange; id NAME &> /dev/null  ||  echo "NAME is not exist"
wange is not exist
[root@centos8 ~]#NAME=wange; id NAME &> /dev/null  && echo "NAME is exist" ||  echo "NAME is not exist"
wange is not exist
[root@centos8 ~]#NAME=wang; idNAME &> /dev/null  && echo "NAME is exist" ||  echo "NAME is not exist"
wang is exist
[root@centos8 ~]#NAME=wang; id NAME &> /dev/null  && echo "NAME is exist" ||  echo "NAME is not exist"
wang is exist
[root@centos8 ~]#NAME=wang; idNAME &> /dev/null  ||  echo "NAME is not exist" && echo "NAME is exist"
wang is exist
[root@centos8 ~]#NAME=wange; id NAME &> /dev/null  ||  echo "NAME is not exist" && echo "NAME is exist"
wange is not exist
wange is exist

#结论:如果&& 和 || 混合使用,&& 要在前,|| 放在后

[root@centos8 ~]#NAME=wange; idNAME &> /dev/null  && echo "NAME is exist" ||  useraddNAME
[root@centos8 ~]#id wange
uid=1002(wange) gid=1002(wange) groups=1002(wange)

[root@centos8 ~]#NAME=wangge; id NAME &> /dev/null  && echo "NAME is exist" || ( useradd  NAME; echoNAME is created )
wangge is created
[root@centos8 ~]#id wangge
uid=1003(wangge) gid=1003(wangge) groups=1003(wangge)

[root@centos8 ~]#NAME=wanggege; id NAME &> /dev/null  && echo "NAME is exist" || { useradd  NAME; echoNAME is created; }
wanggege is created

范例:网络状态判断

[root@centos8 ~]#cat /data/scripts/ping.sh
#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#QQ:            29308620
#Date:          2019-12-23
#FileName:      ping.sh
#URL:           http://www.magedu.com
#Description:       The test script
#Copyright (C):     2019 All rights reserved
#********************************************************************
IP=172.16.0.1
ping -c1 -W1 IP &> /dev/null  && echo "IP  is up" || {  echo "$IP  is unreachable"; exit; }
echo "Script is finished"
[root@centos8 ~]#bash /data/scripts/ping.sh
172.16.0.1  is up
Script is finished

范例:磁盘空间的判断

[root@centos8 ~]#cat /data/script40/disk_check.sh 
#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#QQ:            29308620
#Date:          2019-12-23
#FileName:      disk_check.sh
#URL:           http://www.magedu.com
#Description:       The test script
#Copyright (C):     2019 All rights reserved
#********************************************************************
WARNING=80
SPACE_USED=df|grep '^/dev/sd'|tr -s ' ' %|cut -d% -f5|sort -nr|head -1
[ "SPACE_USED" -geWARNING ] && echo "disk used is $SPACE_USED,will be full"  | mail -s diskwaring root

范例:磁盘空间和Inode号的检查脚本

[root@centos8 scripts]#cat disk_check.sh
#!/bin/bash
#
#********************************************************************
#Author:        wangxiaochun
#QQ:            29308620
#Date:          2020-04-03
#FileName:      disk_check.sh
#URL:           http://www.magedu.com
#Description:       The test script
#Copyright (C):     2020 All rights reserved
#********************************************************************
WARNING=80
SPACE_USED=df  | grep '^/dev/sd'|grep -oE '[0-9]+%'|tr -d %| sort -nr|head -1
INODE_USED=df -i | grep '^/dev/sd'|grep -oE '[0-9]+%'|tr -d %| sort -nr|head -1
[ "SPACE_USED" -gtWARNING -o "INODE_USED" -gtWARNING ] && echo "DISK USED:SPACE_USED%, INODE_USED:INODE_USED,will be full" | mail -s "DISK Warning" root@wangxiaochun.com

本文链接:http://www.yunweipai.com/34334.html

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/52548.html

(0)
上一篇 2021年8月6日
下一篇 2021年8月6日

相关推荐

发表回复

登录后才能评论