6. Conditional testing and comparison of shell scripts
(1) Common syntax for conditional expressions
1. Six ways to write conditional expressions (if, while)
Syntax 1:test<test expression>
Syntax 2: [<Test Expression>] #Both ends of brackets must have spaces
Syntax 3: [[[<Test Expression>] #Spaces are required at both ends
Syntax 4: ((Test Expression))#Spaces are not required at both ends
Syntax 5: (command expression)
Syntax 6: Command expression
Application Display
①[]Conditional expression [root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# [ -e /etc/host1 ] && echo 0 || echo 1 1 ②test Conditional expression: test and[]Same function [root@centos6-kvm3 scripts]# test -e /etc/host1 && echo 0 || echo 1 1 [root@centos6-kvm3 scripts]# test -e /etc/hosts && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# # [] == test ③[[]]Double middle bracket conditional expression requiring spaces on both sides [root@centos6-kvm3 scripts]# [[ -e /etc/host1 ]] && echo 0 || echo 1 1 [root@centos6-kvm3 scripts]# [[ -e /etc/hosts ]] && echo 0 || echo 1 0 ④Double bracket conditional expression, no spaces on either side [root@centos6-kvm3 scripts]# (( -e /etc/hosts )) && echo 0 || echo 1 -bash: ((: -e /etc/hosts : division by 0 (error token is "/hosts ") 1 ⑤Double brackets are generally used to calculate: [root@centos6-kvm3 scripts]# ((3>5)) && echo 0 || echo 1 1 [root@centos6-kvm3 scripts]# ((3<5)) && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# #(()) for calculation ⑥expr Expression: Use parentheses [root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# (expr 1 + 2 &>/dev/null) && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# (expr 1 + a &>/dev/null) && echo 0 || echo 1 1 ⑦expr Expression: Use quotation marks [root@centos6-kvm3 scripts]# `expr 1 + a &>/dev/null` && echo 0 || echo 1 1 [root@centos6-kvm3 scripts]# `expr 1 + 2 &>/dev/null` && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]#
(2) The editing grammar of conditional expressions:
1, [<Test Expression>] &&Command 1 ||Command 2
If the previous expression succeeds, execute command 1, otherwise execute command 2
If [<test expression>] then Command 1 else Command 2 fi
2. Multiple Command Conditions
When there are many commands, we can use curly brackets to enclose all the commands.The following:
[<Test Expression>] & & & {
Command 1
Command 2
}||{
Command 3
Command 4
}
3. Keep only those that execute successfully
[<Test Expression>] & &{
Command 1
Command 2
Command 3
}
4. Keep only those execution failures
[<Test Expression>] | {
Command 1
Command 2
Command 3
}
(3) File test expression
man test
-d:Files are directories and exist [root@centos6-kvm3 scripts]# [ -d /etc/hosts ] && echo 0 || echo 1 1 [root@centos6-kvm3 scripts]# [ -d /etc ] && echo 0 || echo 1 0 -f: Judges as file and exists [root@centos6-kvm3 scripts]# [ -f /etc/hosts ] && echo 0 || echo 1 0 -e: Determine existence, directory or file [root@centos6-kvm3 scripts]# [ -e /etc/hosts ] && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# [ -e /etc ] && echo 0 || echo 1 0 -r: Determine that the file is readable: [root@centos6-kvm3 scripts]# [ -r /etc/hosts ] && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# ll /etc/hosts -rw-r--r--. 2 root root 352 Nov 19 2018 /etc/hosts -x:Determine that the file is executable: [root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1 1 [root@centos6-kvm3 scripts]# chmod +x /etc/hosts [root@centos6-kvm3 scripts]# [ -x /etc/hosts ] && echo 0 || echo 1 0 -s: Determine that the file size is not zero: [root@centos6-kvm3 scripts]# [ -s /etc/hosts ] && echo 0 || echo 1 0 [root@centos6-kvm3 scripts]# touch oldboy.log [root@centos6-kvm3 scripts]# [ -s oldboy.log ] && echo 0 || echo 1 1 //Example application: crond [root@centos6-kvm3 scripts]# cat /etc/init.d/crond
(4) Explanation of common functions of string test expressions
n:not zero ,[-n "Character string" ] String length is not zero, expression is true. z:zero,[-z "Character string" ] The string length is 0 and the expression is true. ["String 1"=="String 2"] Two strings are true if they are identical. ["String 1"!="String 2"] Two strings that are not identical are true. //Be careful: 1,The string is double quoted. 2,An equal sign can be one or two. 3,There must be spaces at both ends of the equal sign. [root@centos6-kvm3 scripts]# [ -n "oldboy" ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [ -z "oldboy" ] && echo 1 || echo 0 0 [root@centos6-kvm3 scripts]# char="oldboy" [root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# unset char [root@centos6-kvm3 scripts]# [ -n "$char" ] && echo 1 || echo 0 0 [root@centos6-kvm3 scripts]# [ "dd"=="ff" ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [ "dd" == "ff" ] && echo 1 || echo 0 0 [root@centos6-kvm3 scripts]# [ "dd" == "dd" ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [ "dd" != "ff" ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [ "dd" != "dd" ] && echo 1 || echo 0 0 //Instance application: cat /etc/init.d/crond cat /etc/init.d/network
Example:
[root@centos6-kvm3 scripts]# cat select1.sh #!/bin/bash cat << EOF 1.install lamp 2.install lnmp 3.exit EOF read -p "Please enter a serial number:" num [ -z "$num" ] && exit 1 #Determine if the content is empty expr 2 + $num &>/dev/null if [ $? -ne 0 ] then echo "usage:$0{1|2|3}" exit 1 fi if [ $num -eq 1 ] then echo "install lamp..." elif [ $num -eq 2 ] then echo "install lnmp ..." elif [ $num -eq 3 ] then echo "bye..." exit else echo "usage:$0{1|2|3}" exit 1 fi
(5) Integer test expression
Comparison expressions used in [] and test | Comparison symbols used in (()) and [[]] | Explain |
---|---|---|
-eq | ==or= | equal to |
-ne | != | not equal to |
-gt | > | greater then |
-ge | >= | Greater than or equal to |
-lt | < | Less than then |
-le | <= | Less than or equal to |
Example
[root@centos6-kvm3 ~]# [ 2 -eq 3 ] && echo 0 || echo 1 1 [root@centos6-kvm3 ~]# [ 2 -gt 3 ] && echo 0 || echo 1 1 [root@centos6-kvm3 ~]# [ 2 -lt 3 ] && echo 0 || echo 1 0 [root@centos6-kvm3 ~]# [ 2 > 3 ] && echo 0 || echo 1 0 [root@centos6-kvm3 ~]# [ 2 \> 3 ] && echo 0 || echo 1 1 [root@centos6-kvm3 ~]# [[ 2 > 3 ]] && echo 0 || echo 1 1 [root@centos6-kvm3 ~]# [[ 2 -gt 3 ]] && echo 0 || echo 1 1 [root@centos6-kvm3 ~]# [[ 2 -lt 3 ]] && echo 0 || echo 1 0 [root@centos6-kvm3 ~]# (( 2 -lt 3 )) && echo 0 || echo 1 -bash: ((: 2 -lt 3 : syntax error in expression (error token is "3 ") 1 [root@centos6-kvm3 ~]# (( 2 > 3 )) && echo 0 || echo 1 1 [root@centos6-kvm3 ~]# (( 2 < 3 )) && echo 0 || echo 1 0 [root@centos6-kvm3 ~]# test 2 -gt 3 && echo 0 || echo 1 1 [root@centos6-kvm3 ~]# test 2 -lt 3 && echo 0 || echo 1 0 //Summary: 1,Use alphabetic expressions in double brackets. 2,Double parentheses are not suitable for alphabetic expressions, only for symbolic expressions. 3,test Expressions only apply to symbolic expressions.
(6) Test questions: Use read's interaction to compare the size of two integers.
[root@centos6-kvm3 scripts]# cat test3.sh #!/bin/bash read -p "Please enter two integers:" a b [ -z "$b" ] && { echo "Please enter two integers." exit 1 } expr $a + $b + 1 &>/dev/null [ $? -ne 0 ] && { echo "Please enter two integers." exit 2 } [ $a -lt $b ] && { echo "$a less than $b." exit 0 } [ $a -gt $b ] && { echo "$a greater than $b." exit 0 } [ $a -eq $b ] && { echo "$a Be equal to $b." exit 0 } ================ //Use the if statement: [root@centos6-kvm3 scripts]# cat test4.sh #!/bin/bash read -p "Please enter two integers:" a b [ -z "$b" ] && { echo "Please enter two integers." exit 1 } expr $a + $b + 1 &>/dev/null [ $? -ne 0 ] && { echo "Please enter two integers." exit 2 } if [ $a -lt $b ] then echo "$a less than $b." elif [ $a -gt $b ] then echo "$a greater than $b." else echo "$a Be equal to $b." fi [root@centos6-kvm3 scripts]#
(7) Logical test expression
Use operators in [] and test | Use operators in [[]] and (()) | Explain |
---|---|---|
-a | && | and with |
-o | || | Or or |
! | ! | Notnon |
Example:
[root@centos6-kvm3 scripts]# [ 1 -eq 1 -a -f /etc/hosts ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [ 1 -eq 2 -a -f /etc/hosts ] && echo 1 || echo 0 0 [root@centos6-kvm3 scripts]# [ 1 -eq 2 -o -f /etc/hosts ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [ 1 -eq 2 ] -o [ -f /etc/hosts ] && echo 1 || echo 0 -bash: [: too many arguments 0 [root@centos6-kvm3 scripts]# [ 1 -eq 2 ] || [ -f /etc/hosts ] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [ 1 -eq 2 ] && [ -f /etc/hosts ] && echo 1 || echo 0 0 [root@centos6-kvm3 scripts]# [[ 1 -eq 2 || -f /etc/hosts ]] && echo 1 || echo 0 1 [root@centos6-kvm3 scripts]# [[ 1 -eq 2 && -f /etc/hosts ]] && echo 1 || echo 0 0