首页 Shell教程shell条件

shell运算

shell用户环境配置

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

组合测试条件

第一种方式 [ ]
[ 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 -x $FILE ] 
[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 ~]#[ -f $FILE -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 -x $FILE ] 
[root@centos8 ~]#echo $?
0
[root@centos8 ~]#[  -x $FILE ] 
[root@centos8 ~]#echo $?
1
[root@centos8 ~]#[ ! -x $FILE ] 
[root@centos8 ~]#echo $?
0
[root@centos8 ~]#! [ -x $FILE ] 
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 +x $FILE

[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 && echo $IP is up || echo $IP is down
10.0.0.111 is down
[root@centos8 ~]#IP=10.0.0.1;ping -c1 -W1 $IP  &> /dev/null && echo $IP is up || echo $IP 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; id $NAME &> /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; id $NAME &> /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; id $NAME &> /dev/null  && echo "$NAME is exist" ||  useradd  $NAME
[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; echo $NAME 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; echo $NAME 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" -ge $WARNING ] && 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" -gt $WARNING -o "$INODE_USED" -gt $WARNING ] && echo "DISK USED:$SPACE_USED%, INODE_USED:$INODE_USED,will be full" | mail -s "DISK Warning" root@wangxiaochun.com

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

shell运算

shell用户环境配置

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部