1, Branch statement (1) IF-THEN-FI statement: Format:
if Then commands fi
When the command command exit status code is $?=0, enter the branch, or continue with the following command.
(2) IF-THEN-ELSE-FI statement: Format:
if Then commands1 else commands2 fi
When the command command exit status code is $?=0, go to then branch, otherwise execute the Else Branch command. (3) IF-THEN-ELIF-THEN-FI statement: Format:
if Then commands1 elifthen commands2 fi
Nested processing of If branches. 2, the test command can only be tested by the test command with the command exit code independent conditions. The test command provides a way of testing different conditions in a if-then statement. The test command can determine three types of conditions, numeric comparisons, string comparisons, and file comparison formats:
if Then commands fi
Another way to declare the test command in a IF-THEN-FI statement is provided in the bash shell:
if [condition]; then commands fi
square brackets define the conditions used in the test command, note: there must be a space between the left and right brackets (1) numeric comparison N1-EQ n2 &NB condition Sp Check if N1 equals n2 n1-ge n2 Check N1 is greater than or equal to n2 N1 -GT n2 Check if N1 is greater than n2 n1-le n2 Check N1 is less than or equal to n2 N1-LT n2 Check N1 is less than n2 n1-ne n2 Check N1 is not equal to n2   ; Note: Numeric comparisons can only be used to process integers, cannot handle floating-point numbers (2) string comparisons STR1 = str2 Check if STR1 is the same as str2 & nbsp STR1! = str2 Check str1 is different from str2 STR1 < str2 &N Bsp Check if STR1 is smaller than str2 STR1 > str2 Check str1 is greater than str2 &NBS P -N str Check str length is not 0 -Z str Check str length is 0 Note: when using greater than and less than You need to use the escape character \< or \>, otherwise the uppercase letters will be processed as lowercase when you compare the redirect symbol conflict , but when you compare the same string, Small Letter larger (3) file comparison -D file Check if file exists and is directory -E File Check if file exists -F file Check if file exists and is nbsp -R file Check if file exists and is readable -s file & nbsp Checks if file exists and is not empty -W file Check if file exists and can be written &NB Sp -X file Check file exists and executable -o file Check if file exists and is current user all -G file Check if file exists and default group is the same as current user File1-nt file2 &NBSp Check if File1 is newer than file2 File1-ot file2 Check file1 is better than file2 old (4) qualifying test & nbsp The IF-THEN-FI statement allows the use of Boolean logic to combine tests, two Boolean logic symbols available: [condition1] && [conditio N2] [Condition1] | | [Condition2] 3, If-then advanced Features (1) using the double parenthesis format: (expression)) Expressio n can be any mathematical assignment or comparison expression, except for the math operator in the test command, the following symbols: val++/val-- post-increment/post-subtract & nbsp ++val/--val Pre-add/forward minus ! logic take-back ~ &nbs P Click-to-reverse * &NBS P power operations << / >> Bitwise LEFT SHIFT/Right shift &/| by position and/or &&/| | logic and/or NOTE: Mathematical operators for double parentheses do not need to be escaped (2) use both brackets Format: [[Expression]] expression can use the standard string comparison in the test command and provide a pattern match that can be matched using regular expressions & nbsp Example:
if Then commands fi
Matches whether the $USER starts with R and, if passed, executes the commands content. 4. Case Statement Format:
Case inch | val2) Command1;; Val3 Command2; *) default command3;; Esac
To solve the problem of multi-layered nesting of if-then
Shell script Programming-structured commands 1