| WhateverProgrammingLanguage is inseparable from conditional judgment. Shell is no exception.
If list then
Do something here
Elif list then
Do another thing here
Else
Do something else here
Fi
Ex1:
#! /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 # ifend
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: 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 file: the regular file is true.
-D file: the directory is true.
-C file: the special character file is true.
-B file: The Block special file is true.
-S file is true if the file size is not 0
-T file: this parameter is true when the specified device is a terminal in the file descriptor (1 by default ).
3. complex logic judgment
-A and
-O or
! Non
End
Although the syntax is simple, its functions become powerful when used in shell. |