Structured commands: Allow scripts to skip or execute these commands cyclically based on the conditions of variable values or the results of other commands
1. Use the if_then statement
If command
Then
Commands
Fi
Run the command after if first. If the exit status of the command is 0 (the command is successfully executed), all the commands after then and before FI will be executed. Otherwise, it will jump to the back of the FI and continue execution.
2. If-then-else statement
If command
Then
Commands
Else
Commands
Fi
3. nested if statement
If command1
Then
Commands
ElifCommand2
Then
Commands
Elif command3
Then
Commands
Fi
4. Test command (square brackets [] are synonyms)
Used to provide conditions for judgment
If test condition, you can use [conditon] instead of test condition.(There must be spaces before and after)
Then
Commands
Fi
There are three condition types:
(1) numerical comparison:-EQ,-ne,-Ge,-GT,-Le, lt
Note:The test command cannot process the floating point value stored in the variable.
When using the bash calculator BC, it only spoofs shell to store floating point values as string values in a variable. This method is good if you only use the echo statement to display the result first. But it does not work in Numeric-oriented functions (such as numerical test conditions.
The bottom line is that you cannot use non-integer variables in test.
(2) string comparison: = ,! =, <,>,-N (check whether the string length is greater than 0),-z (check whether the string length is equal to 0)
Equal strings: All punctuation marks and uppercase letters are taken into account for test comparison.
String order: Pay attention to two points:
1) '>', '<' must be escaped with '\'. Otherwise, shell uses them as redirection symbols and regards string values as file names.
2) The order of greater than or less than is different from that of the sort command.
In test, the same letter, capital letter> lower-case letter
In sort, the opposite is true.
String size: it is convenient to use-N and-Z to evaluate whether a variable contains data.
The length detected for null and uninitialized variables is also 0.
(3) file comparison
File comparison is the most powerful and commonly used comparison in shell scripts.
Test can test the File status and path. (Frequently used !)
-D file: Check whether the file exists and is a directory
-E file: Check whether the file exists.
-F file: Check whether the file exists and is a file.
-R file: Check whether the file exists and is readable.
-S file: Check whether the file exists and is not empty.
-W file: Check whether the file exists and can be written.
-X file: checks whether the file exists and is executable.
-O file: Check whether the file exists and is owned by the current user.
-G file: Check whether the file exists and whether the default group is the current user group.
File1-nt file2: Check if file1 is newer than file2
File1-ot file2: Check if file1 is older than file2
5. Compound condition Query
[Condition1] & [condition2]
[Condition1] | [condition2]
6. advanced features of if-then
(1) use parentheses to represent Mathematical Expressions
(Expression ))
Expression includes the following operators except standard mathematical operators:
++ ,-,!, ~, **, <, >>,&, |, & |
(2) Use brackets to indicate advanced string processing functions
[[Expression]
Besides the comparison of the Flag strings in the test commandPattern MatchingFunction
In pattern matching, you can define regular expressions that match string values.
7. Case command
You can use the case command instead of writing all Elif statements to continue to check the same variable value.
The case command checks multiple values of a single variable in a list-oriented manner.
Case variable in
Pattern1 | pattern2) commands1 ;;
Pattern3) commands2 ;;
*) Default commands ;;
Esac