The syntax structure is as follows:
1. If-then statements
IF-thenstatement if command #根据conmmand的退出状态码, select Execute statement then commandsfie.g.#! usr/bin/Bashtestuser=Richif grep $testuser/etc/passwdthen for user $testuser is: -a/home/$testuser/fi
2. If-then-else statements
1 #!usr/bin/bash2if command #根据conmmand的退出状态码, select Execute statement 3 Then 4 Commands5Else6 commands7 fi
3. Nesting if (elif)
1#!usr/bin/Bash2 ifCommand3 Then4 commands5 elif Command6 Then7 commands8 elif Command9 ThenTen commands One Else A commands -Fi
4. Test command (single testing condition)
1 # There are 3 main types of test conditions2*Numerical comparison3*string comparison4*file Comparison5 6#usr/bin/Bash7 iftest Condition8 Then9 commandsTen fi One A#!usr/bin/Bash - if[condition] # condition before and after space - Then the commands -Fi
5. Compound condition test
1 # and test: Two conditions must be met (true)2 if[Condition1] &&[Condition2]3 Then4 commands5 fi6 7 8 #OR测试: Satisfies any one of the conditions (either true)9 if[Conditon1] | |[Condition2]Ten Then One commands AFi
6. If-then Advanced Features
1 # Double parenthesis: for mathematical expressions2 ((expression))3 4 if((expression))5 Then6 commands7 ((expression))8 fi9 Ten # Two-way brackets: for string expressions (e.g. regular expressions) One [[expression]] A - if[[expression]] - Then the commands -Fi
7. Case command
1 # Compare the specified variable to a different pattern 2 3 4 Case inch 5 Pattern1 | pattern2) 6 commands1;; 7 pattern3) 8 commands2;; 9 *) commands3;; Esac
[Linux] Structured command IF