7. Conditional judgment
Conditional judgment syntax format
Format 1: test Conditional expression Format 2:[ Conditional expression ] #Pay attention to the spaces Format 3:[[Conditional expression]] #regular expression
Relevant parameters of condition judgment
Q: what do you want to judge?
A: I want to judge the file type, whether the file is old or new, whether the file exists, and whether the strings are equal.....
Judge file type
Red characters are the most commonly used
Judgment parameters | meaning |
---|---|
-e | Determine whether the file exists (any type of file) |
-f | Determine whether the file exists and is an ordinary file |
-d | Determine whether the file exists and is a directory |
-L | Determine whether the file exists and is a soft connection file |
-b | Determine whether the file exists and is a block device file |
-S | Determine whether the file exists and is a socket file |
-c | Determine whether the file exists and is a character device file |
-p | Determine whether the file exists and is a named pipe file |
-s | Judge whether the file exists and is a non empty file (with content) |
For example: judge whether the file exists? (key points)
[root@localhost ~]# touch pokes.txt [root@localhost ~]# test -e pokes.txt [root@localhost ~]# echo $? 0 [root@localhost ~]# test -e pokessss.txt [root@localhost ~]# echo $? 1 [root@localhost ~]# [ -e pokes.txt ] #Brackets are also OK. Brackets are often used in scripts. Remember that the spaces at both ends of brackets cannot be less [root@localhost ~]# echo $? 0
Judge file permissions
Red characters are the most commonly used
Test options | effect |
---|---|
-r file | Judge whether the file exists and whether the file has read permission. If yes, it is true |
-w file | Judge whether the file exists and whether the file has write permission. If yes, it is true |
-x file | Judge whether the file exists and whether the file has execution permission. If yes, it is true |
-u file | Judge whether the file exists and whether the file has SUID permission. If yes, it is true |
-g file | Judge whether the file exists and whether the file has SGID permission. SGID permission is true |
-k file | Judge whether the file exists and whether the file has SBit permission. It is true to have SBit permission |
For example: judge whether the file has write and execute permission?
[root@localhost ~]# ll Total consumption 4 -rw-------. 1 root root 1241 9 May 20, 2020 anaconda-ks.cfg -rw-r--r--. 1 root root 0 1 October 28:51 pokes.txt [root@localhost ~]# [ -w pokes.txt ] [root@localhost ~]# echo $? 0 [root@localhost ~]# [ -x pokes.txt ] [root@localhost ~]# echo $? 1 [root@localhost ~]#
Comparison between two files (old and new)
option | effect |
---|---|
File 1 -nt file 2 | Judge whether the modification time of file 1 is newer than that of file 2. If it is new, it is true |
File 1 -ot file 2 | Judge whether the modification time of file 1 is older than that of file 2. If it is older, it is true |
File 1 -ef file 2 | Judge whether the Inode numbers of file 1 and file 2 are consistent. If they are consistent, it is true. It can be understood as whether the two files are the same file. This judgment is a good way to judge the hard link. |
Comparison between two integers
Red characters are the most commonly used
Test options | effect |
---|---|
Integer 1 -eq integer 2 | Judge whether integer 1 is equal to integer 2. Equality is true |
Integer 1 -ne integer 2 | Judge whether integer 1 is not equal to integer 2. If not, it is true |
Integer 1 -gt integer 2 | Judge whether integer 1 is greater than integer 2, and greater than is true |
Integer 1 -lt integer 2 | Judge whether integer 1 is less than integer 2, and less than is true |
Integer 1 -ge integer 2 | Judge whether integer 1 is greater than or equal to integer 2, and greater than or equal to is true |
Integer 1 -le integer 2 | Judge whether integer 1 is less than or equal to integer 2, and less than or equal to is true |
For example: judge whether two integers are equal?
[root@localhost ~]# [ 23 -eq 24 ] [root@localhost ~]# echo $? 1 [root@localhost ~]# [ 24 -eq 24 ] [root@localhost ~]# echo $? 0 [root@localhost ~]#
String judgment
option | effect |
---|---|
-z string | Judge whether the string is empty. If it is empty, it returns true |
-n string | Judge whether the string is not empty. If it is not empty, it returns true |
String 1 = = string 2 (two equal signs) | Judge whether the strings are equal and return true if they are equal |
String 1== String 2 (two equal signs) | Judge whether the strings are not equal. If not, it returns true |
For example: judge whether two strings are equal? (key points)
[root@localhost ~]# [ "aa" == "aa" ] #Note that the string has spaces at both ends of the equal sign [root@localhost ~]# echo $? 0 [root@localhost ~]# [ "aa" == "bb" ] [root@localhost ~]# echo $? 1
Multiple conditional judgment
option | effect |
---|---|
Judgment 1 -a judgment 2 | Both logic and judgment 1 and judgment 2 are true, and the final result is true |
Judgment 1 -o judgment 2 | Logical or, if one of judgment 1 and judgment 2 holds, the final result is true |
! judge | Logical negation, which reverses the original judgment |
For example: judge whether 11 exists, and then judge whether 11 is greater than 23?
[root@localhost ~]# [ -n 11 -a 11 -gt 23 ] #Judge whether 11 exists, and then judge whether 11 is greater than 23. The integer must exist. [root@localhost ~]# echo $? 1 [root@localhost ~]# [ -n 11 -a 11 -gt 9 ] [root@localhost ~]# echo $? 0