No matter what programming language, the linuxif command cannot be separated from conditional judgment. SHELL is no exception. Iflistthendosomethinghereeliflistthendoanotherthinghereelsedosomethingelseherefi #! /Bin/shSYS linux if command
No matter what programming language, conditional judgment is indispensable. SHELL is no exception.
If list
Then
Do something
Here
Elif list
Then
Do another thing
Here
Else
Do something else here
Fi
#! /Bin/sh
SYSTEM = 'uname-s' # obtain the operating SYSTEM type.
If [$ SYSTEM = "Linux"]; then
# If it is linux, print the linux string
Echo "Linux"
Elif [$ SYSTEM = "FreeBSD"];
Then
Echo "FreeBSD"
Elif [$ SYSTEM = "Solaris"];
Then
Echo "Solaris"
Else
Echo "What? "
Fi
# If end
It is basically the same as other scripting languages. There is no big difference. However, it is worth noting that. [] Condition judgment.
1 string judgment
Str1 = str2: true when two strings have the same content and length
Str1! = Str2: true when str1 and str2 are different
-N
Str1 is true when the length of a string is greater than 0 (the string is not empty)
-Z str1
True (empty string) when the string length is 0)
Str1 is true when str1 is not empty
2. digit judgment
Int1-eq int2 two numbers are equal to true
Int1-ne int2 two numbers are not true
Int1-gt
Int2 int1 greater than int2 true
Int1-ge int2 int1 greater than or equal to int2 true
Int1-lt
Int2 int1 smaller than int2 true
Int1-le int2 int1 smaller than or equal to int2 true
3. file judgment
-R file user readable to true
-W file users can write to true
-X file user executable is true
-F
The file is a regular file and is true.
-D file: The Directory is true.
-C file: The special character file is true.
-B
The file is a special block file that is true.
-S file is true if the file size is not 0
-T
File is true when the device specified by the file descriptor (1 by default) is a terminal.
3. complex logic judgment
-A and
-O or
! Non
End
Although the syntax is simple, its functions become powerful when used in SHELL.