Chapter 2: using structured commands
Use the if-then statement
if commandthen commandsfi
If the exit status code of the command after if is 0, execute then.
Another Form
if command; then commandsfi
If-then-else statement
if commandthen commandselse commandsfi
If the exit status code of the command after if is 0, execute then. Otherwise, run Else.
Nested if (else if)
if command1then commandselif command2then commandsfi
Test command
The test command can determine three types of conditions.
1. Numerical Comparison
2. String comparison
3. file comparison
Format:
TestCondition
In the if statement, square brackets [] can be used.
if test conditionthen commandsfi
Or
if [ command ]then commandsfi
Note: a space must be added on both sides of square brackets. Otherwise, an error will be reported.
Numerical Comparison
Comparison |
Description |
N1-EQ N2 |
Check whether N1 is equal to N2 |
N1-ge N2 |
Check whether N1 is greater than or equal to N2 |
N1-GT N2 |
Check whether N1 is greater than N2 |
N1-Le N2 |
Check whether N1 is raining or equal to N2 |
N1-lt N2 |
Check whether N1 is less than N2 |
N1-ne N2 |
Check whether N1 is not equal to N2 |
It can be used on variables.
Note: floating point numbers cannot be used.
String comparison
Comparison |
Description |
Str1 = str2 |
Check if str1 is the same as str2 |
Str1! = Str2 |
Check if str1 is different from str2 |
Str1 <str2 |
Check if str1 is smaller than str2 |
Str1> str2 |
Check if str1 is larger than str2 |
-N str1 |
Check if the str1 length is not 0 |
-Z str1 |
Check whether the str1 length is 0 |
Note the following for sequence comparison:
1. Escape if the value is greater than or less than the symbol, otherwise it will be recognized as a redirection symbol.
2. The test command is sorted in ASCII order, which is different from the sort command.
String Length
If the variable is a null string or is not initialized, the variable length is 0.
Empty strings or uninitialized variables may have disastrous effects on the program. before using them, you 'd better use-N and-Z in the test command to confirm
File comparison
Comparison |
Description |
-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 |
Check whether the file exists. |
-W |
Check whether the file exists and can be written |
-X |
Check whether the file exists and can be executed |
-O |
Check whether the file exists and belongs to the current user |
-G |
Check whether the file exists and the default group is the same as the current user. |
File1-nt file2 |
Check if file1 is newer than file2 |
File1-ot file2 |
Check if file1 is older than file2 |
Take the check directory as an example:
if [ -d $HOME ]then cd $HOMEelse echo "$HOME is not a directory"fi
Composite condition test
[Condition1] | [condition2]
[Condition1] & [condition2]
Advanced features of if-then
1. parentheses used for mathematical expressions
2. brackets used for advanced string processing
Parentheses allow advanced mathematical expressions to be placed in comparison
Format:
(Expression ))
Command symbols
Symbol |
Description |
Val ++ |
Post-Increment |
Val -- |
Subtraction |
++ Val |
Add first |
-- Val |
Minus |
! |
Logic inversion |
~ |
Bitwise Inversion |
** |
Power Operation |
< |
Left Shift |
> |
Right Shift |
& |
Boolean and |
| |
Boolean OR |
&& |
Logic and |
| |
Logic or |
Brackets
[[Expression]
Provides the pattern matching feature, where you can use regular expressions
if [ $USER == r* ]
Case command
Command Format:
case variable inpattern1 | pattern2) commands1;;pattern3) commands2;;*) default commands;;esac
In Bash, you can check strings in case.
From Java to bash, the syntax is still very large, but the idea will not change.
The following is a simple example:
case $test inpork | beef | lamb ) echo "$test is meat!" echo "I like it!";;potato | tomato | eggplant ) echo "$test is a vegetable" echo "who likes it?";;*) echo "what's this?";;esac
Before Java 7, this write is not allowed. The switch case does not support the string type.
Repost the following link
My blog address
Http://su1216.iteye.com/
Http://blog.csdn.net/su1216/