Conditional judgment in shell script

Posted by pwicks on Sat, 29 Jan 2022 11:30:55 +0100

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 parametersmeaning
-eDetermine whether the file exists (any type of file)
-fDetermine whether the file exists and is an ordinary file
-dDetermine whether the file exists and is a directory
-LDetermine whether the file exists and is a soft connection file
-bDetermine whether the file exists and is a block device file
-SDetermine whether the file exists and is a socket file
-cDetermine whether the file exists and is a character device file
-pDetermine whether the file exists and is a named pipe file
-sJudge 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 optionseffect
-r fileJudge whether the file exists and whether the file has read permission. If yes, it is true
-w fileJudge whether the file exists and whether the file has write permission. If yes, it is true
-x fileJudge whether the file exists and whether the file has execution permission. If yes, it is true
-u fileJudge whether the file exists and whether the file has SUID permission. If yes, it is true
-g fileJudge whether the file exists and whether the file has SGID permission. SGID permission is true
-k fileJudge 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)

optioneffect
File 1 -nt file 2Judge 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 2Judge 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 2Judge 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 optionseffect
Integer 1 -eq integer 2Judge whether integer 1 is equal to integer 2. Equality is true
Integer 1 -ne integer 2Judge whether integer 1 is not equal to integer 2. If not, it is true
Integer 1 -gt integer 2Judge whether integer 1 is greater than integer 2, and greater than is true
Integer 1 -lt integer 2Judge whether integer 1 is less than integer 2, and less than is true
Integer 1 -ge integer 2Judge whether integer 1 is greater than or equal to integer 2, and greater than or equal to is true
Integer 1 -le integer 2Judge 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

optioneffect
-z stringJudge whether the string is empty. If it is empty, it returns true
-n stringJudge 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

optioneffect
Judgment 1 -a judgment 2Both logic and judgment 1 and judgment 2 are true, and the final result is true
Judgment 1 -o judgment 2Logical or, if one of judgment 1 and judgment 2 holds, the final result is true
! judgeLogical 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

Topics: Linux bash