This is the 24th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
preface
Control statements require logical expressions to branch. Bash shell logical expressions can be written in three ways: test expression, [expression], or [[expression]].
test
The format of the test command is test expression. Expression is our logical expression, which returns 0 if true or non-0 if false. Example:
#! /bin/bash
# 1 = = 1
test 1 -eq 1
echo "1 == 1: $?"
# 1 = = 2
test 1 -eq 2
echo "1 == 2: $?"
Copy the code
Execution Result:
Note that the test command has a fixed way of writing value comparisons (it’s a command, after all), and falls into three main categories: numeric comparisons, string comparisons, and file type checks.
Numeric comparison
Numeric comparisons cannot be made with ==,! =, <, > these symbols compare, and have fixed arguments, as in the example above, 1==1, to be written as 1-eq 1, the following illustration:
Man test = man test = man test = man test
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
Example: test integer1-eq Integer2: Compares the value of integer1 to integer2. The same as other examples
String comparison
String comparisons can be made with = or! = these symbols, along with some non-null check options, are copied from the man test below, with some notes added:
-n STRING: The length of the STRING is greater than 0. The length of STRING is nonzero
-z STRING: The length of the STRING is 0. The length of STRING is zero
If the string is empty (it is not an empty string), both -n and -z are true
STRING1 = STRING2
the strings are equalSTRING1 ! = STRING2 the strings are not equal
Example:
#! /bin/bash
str1="str1"
test -n $str1;echo $?
test -z $str2;echo $?
test -n $str2;echo $?
test x$str2 = x;echo $?
test $str1! ="";echo $?
Copy the code
Execution result (both true) :
File comparison, type/attribute checking
Man test, man test, man test, man test
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbersFILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2FILE1 -ot FILE2
FILE1 is older than FILE2-b FILE
FILE exists and is block special-c FILE
FILE exists and is character special-d FILE
FILE exists and is a directory-e FILE
FILE exists-f FILE
FILE exists and is a regular file-g FILE
FILE exists and is set-group-ID-G FILE
FILE exists and is owned by the effective group ID-h FILE
FILE exists and is a symbolic link (same as -L)-k FILE
FILE exists and has its sticky bit set-L FILE
FILE exists and is a symbolic link (same as -h)-O FILE
FILE exists and is owned by the effective user ID-p FILE
FILE exists and is a named pipe-r FILE
FILE exists and read permission is granted-s FILE
FILE exists and has a size greater than zero-S FILE
FILE exists and is a socket-t FD file descriptor FD is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set-w FILE
FILE exists and write permission is granted-x FILE
FILE exists and execute (or search) permission is granted
Example:
#! /bin/bash
# Whether the file exists
test -e demo.sh
echo "demo.sh exists: $?"
test -e demo1.sh
echo "demo1.sh exists: $?"
Copy the code
Execution Result:
Logic and: -A, logic or: -o
Because the test is a command, so the logic and/or do not like other languages use && and | | and other symbols, these symbols in the shell but have special significance, such as the above example, use && connecting two commands, each one is successful, will perform the next article.
Example:
#! /bin/bash
#1 is 1 and 1 is 2
test 1 -eq 1 -a 1 -eq 2
echo "1 == 1 &&1 == 2: $?"
#1 is 1 or 1 is 2
test 1 -eq 1 -o 1 -eq 2
echo "1 = = 1 | | 1 = = 2: $?"
Copy the code
Execution Result:
[command
Notice “[“, this is a command, usually written as [expression], notice the space before and after expression,” [” is not a symbol, it is a command, it is equivalent to the command of test, so its expression is used in the same way as test.
[,] : [,] : [,] : [,] : [,] : [,] : [,] : [,] : [,] : [
#! /bin/bash
#1 is 1 and 1 is 2
[ 1 -eq 1 -a 1 -eq 1 ]
echo "1 == 1 &&1 == 2: $?"
#1 is 1 or 1 is 2
[ 1 -eq 1 -o 1 -eq 1 ]
echo "1 = = 1 | | 1 = = 2: $?"
Copy the code
Execution Result:
[[ expression ]]
Although “[[” than” [“, more than a “[“, but the meaning is not the same,” [” is a command, but “[[” is a keyword.
However, [[expression]] is much more powerful than [expression]. It is an enhanced version of [expression]. Such as [[expression]] can use && for logic and connector does not support – a, the same logic or | |, but does not support – o. In addition, [[expression]] is more compatible than [expression], and some other abilities are supported, such as using <,> to compare numbers, but because “[” is a command, it is not supported (need to escape).
So, using [[expression]] feels more like programming convention, as follows:
1 #! /bin/bash2, 3,# Numerical calculation comparison,4 [[5 < $((3+3) &&4 >3]] 5echo "[[5 < 3+ 3&&4 >3]] : $?"
6 [] : [] : [] : [] : [] : [] : [7 [5 < $((3+3) &&4 >3]Copy the code
Execution Result:
[5 -lt $((3+3)) -a 4 -gt 3]