组合测试条件
第一种方式 [ ]
1
2
3
|
[
EXPRESSION1 - a EXPRESSION2 ] 并且, EXPRESSION1和 EXPRESSION2都是真,结果才为真
[
EXPRESSION1 - o EXPRESSION2 ] 或者, EXPRESSION1和 EXPRESSION2只要有一个真,结果就为真
[
! EXPRESSION ] 取反 |
说明: -a 和 -o 需要使用测试命令进行,[[ ]] 不支持
范例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
[
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
|
第二种方式 [[ ]]
1
2
3
4
5
6
7
|
COMMAND1
&& COMMAND2 #并且,短路与,代表条件性的AND THEN
如果
COMMAND1 成功 ,将执行 COMMAND2 ,否则 ,将不执行 COMMAND2
COMMAND1
|| COMMAND2 #或者,短路或,代表条件性的OR ELSE
如果
COMMAND1 成功 ,将不执行 COMMAND2 ,否则 ,将执行 COMMAND2
!
COMMAND #非,取反 |
1
|
[
root @ centos7 ~ ] #[ $[RANDOM%6] -eq 0 ] && rm -rf /* || echo "click"click |
范例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[
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 |
范例:
1
2
3
4
5
6
7
8
9
|
[
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 |
范例:&& 和 || 组合使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[
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 |
范例:网络状态判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[
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 |
范例:磁盘空间的判断
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[
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
= < code > 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" | - s diskwaring root |
范例:磁盘空间和Inode号的检查脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
[
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
= < code > df | grep '^/dev/sd' | grep - oE '[0-9]+%' | tr - d % | sort - nr | head - 1
INODE_USED
= < code > 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" | - s "DISK Warning" root @ wangxiaochun .com |
本文链接:http://www.3yyy.top/34334.html