- Conditional Judgments in shell scripts
1. if [Condition];then Statement Fi 2. If [Condition];then Statement elif [Condition];then Statement Else Statement Fi |
Note the point:
1-1. If and fi are paired, a syntax error occurs when the script executes when the fi is missing.
1-2. When the condition is judged, the use of square brackets requires special care, where there must be a space between if and [, there must be a space between the front bracket and the conditional expression,] there must be a space between the back bracket and the conditional expression, which requires attention to these three places.
Example: The script shown in 1, when there is no space between if and [
Figure 1
Its running result 2
Figure 2
Thus, the treatment of square brackets "[]" must be taken seriously.
Loops in a 2.shell script
2-1.for Loop Statements
#!/bin/bash For x in one, three four Do Echo Number $x Done |
Attention:
In this loop, the value of each variable x is a type of value in the list after "in", which can be a file name
2-2.while Loop Statements
#!/bin/bash Var=1 While [$var-le 10] Do Echo $var var=$ (($var + 1)) Done |
Note: It is necessary to determine whether the condition is true before the loop executes.
2-3.until Loop Statements
#!/bin/bash Var=1 Until [$var-GT 10] Do Echo $var var=$ (($myvar + 1)) Done |
The method of self-increment of five kinds of variables
1. i= ' expr $i + 1 ';
2. Let i+=1;
3. ((i++));
4. i=$[$i +1];
5. i=$ (($i + 1))
Note points that require special attention in writing shell scripts