No matter what programming language is inseparable from conditional judgment. The shell is no exception.
The approximate format is as follows:
If List Then
Do something here
Elif List Then
Do another thing here
Else
Do something else here
Fi
An example:
#!/bin/sh
System= ' uname-s ' # get OS type, I'm local Linux
if [$SYSTEM = "Linux"]; Then # if it's Linux session output Linux string
echo "Linux"
elif [$SYSTEM = "FreeBSD"]; Then
echo "FreeBSD"
elif [$SYSTEM = "Solaris"]; Then
echo "Solaris"
Else
echo "What?"
Fi # End of judgment, end with fi
Basically the same as other scripting languages. There's no big difference. But it's worth noting that. [] inside the condition to judge. The description is as follows:
1 string Judgments
STR1 = str2 When two strings have the same content, length is true
Str1! = str2 True when string str1 and str2 are not equal
-N str1 True when the length of the string is greater than 0 (string non-null)
-Z str1 True when the string length is 0 (empty string)
STR1 is True when string str1 is not empty
2 Number of judgements
Int1-eq int2 Two numbers equal to True
Int1-ne Int2 Two number is true
INT1-GT Int2 int1 greater than Int2 is true
Int1-ge Int2 int1 greater than or equal to Int2 true
Int1-lt Int2 int1 Less than Int2 is true
Int1-le Int2 int1 less than or equal to Int2 true
3 file-related if judgment condition statement
-r file user readable as True
-W file user can write as true
-X file user can execute as true
-F file is true for regular files
-d file files are directory-True
-C File file is true for character special files
-B file files are true for block special files
-S file files non-0 o'clock True
-T file is true when the specified device is terminal (default = 1)
3 Complex logic Judgments
-A and
-O or
! Non -
The syntax is simple, but when used in the shell, it can implement powerful functions or execute logic.
Linux Shell If Judgment statement