Syntax format
If Command;then
Commands
Fi
The command contains the following:
Shell command
Any shell command, if the shell command returns 0, represents true, otherwise, represents false. and multiple command can be used as the judgment condition of if, that can be written as:
If Command1;command2;command3;then
Commands
Fi
At this point, if Command3 returns a result of 0, run commands, otherwise not run, although Command1 and Command2 will also run, but the return result of their run has no effect
Test condition or [condition]
The two are equivalent, where the condition can be a numeric comparison, which can be a string comparison, which can be a file comparison:
Number comparison
NUM1 eq num2 equals
Num1 ne num2 Not equal to
NUM1 GT num2 Greater than
Num1 GE num2 greater than or equal
NUM1 LT num2 Less than
Num1 le Num2 is less than or equal to
string comparison
str1 = str2 equals
Str1! = str2 Not equal to
str1 > str2 greater than
STR1 < str2 less than
Whether the length of the-n str str is greater than 0
The length of the-Z str str is 0
File comparison
Whether the-D file file is a directory
Whether the-F file files are
Whether the-e file file exists
-S file file exists and is not empty
-r file file exists and is readable
-W file file exists and is writable
-x file file exists and can be executed
-o file file exists and belongs to the current user
-G file file exists and belongs to the initial group (also called default group) where the current user resides
File1-nt file2 file1 is newer than file2
File1-ot File2 file1 is older than file2
Condition can also be compounded by && or | | Connection:
[Condition1] && [Condition2]
[Condition1] | | [Condition2]
The IF statement in the shell