Quick Start shell script writing (3)
This section describes the control statements and loop statements.
Comments and shell multi-line comments
Shell single-line comment is very simple. You only need to add # At the beginning of each line, but he does not actually provide a dedicated multi-line comment method. Therefore, we use a clever method to comment multiple lines, use the EOF delimiter to comment multiple lines, as shown below:
1 #! /Bin/sh 2 3 file = "/Desktop/file. test" 4 5 <
Lines 5-17 in the above Code are commented out.
If judgment statement
The if judgment statement should be familiar with it. In many examples above, the if judgment statement is used. Here, the two programs are directly used and the running results are provided.
1 #! /bin/sh 2 3 var="test" 4 5 if [ $var == test1 ] 6 then 7 echo "1" 8 else if [ $var == test ] 9 then 10 echo "2" 11 fi 12 fi
Running result: 2
The structure used above is
If...
Then...
Else if...
Then...
Fi
Fi
Another structure is:
1 #! /bin/sh 2 3 var="test" 4 5 if [ $var == test1 ] 6 then 7 echo "1" 8 elif [ $var == test ] 9 then 10 echo "2" 11 fi
The running result is also 2. This structure is similar to the above, but the intermediate else if... fi is changed to elif.
Loop statement
For Loop
Let's take a look at an example:
1 #! /bin/sh 2 3 for (( i=0; i<10; i++)) 4 do echo $i 5 done
At first glance, I am still very familiar with it, but I need to pay attention to the following points:
1. The for loop should be enclosed in double parentheses, that is :((...;...;...;))
2. do and done are essential.
The output result is all numbers from 0 to 9.
While Loop
1 #! /bin/sh 2 3 i=0 4 while((i<10)) 5 do 6 echo $i 7 let i++ 8 done
The function of this loop is the same as that of the for Loop result output above.
The structure of the while loop is:
While...
Do...
Done
Until Loop
The until loop is rarely used. The structure is similar to the do while loop in c/c ++ java, but the actual condition is the termination condition rather than the execution condition in c/c ++. Here is a brief introduction. You can skip this step if you want to browse it.
Structure:
Until...
Do...
Done
1 #! /bin/sh 2 3 i=0 4 until((i>=10)) 5 do 6 echo $i 7 let i++ 8 done
The execution result of the preceding loop is the same as that of the previous one. Note that the condition in the until is the exit condition.
Array
Before reading the for in and case statements, let's take a look at the shell array.
Representation:
Method 1: arraynum = (1 2 3 4 5)
Method 2:
Arraynum [0] = 0
Arraynum [1] = 1
Arraynum [2] = 2
How to get the length of an array: $ {# arraynum [@]} (arraynum [@] is actually a list of arraynum. The counting symbol is described earlier, so this expression is not hard to understand)
For in statement
Next let's look at how to traverse the output Array
1 #! /bin/sh 2 3 arraynum[0]=a 4 arraynum[1]=b 5 arraynum[2]=c 6 7 for var in ${arraynum[@]} 8 do 9 echo ${var} 10 done 11 12 echo ${#arraynum[@]} 13 echo ${arraynum[@]}
The for in statement is used to traverse the output array content. The last two sentences are used to verify that what I mentioned above, $ {# arraynum [@]} is the length of the calculated array, $ {arraynum [@]} is an array list.
Let's take a look at the output:
The structure of the for in statement is as follows:
For variable name in list
Do...
Done
This expression is commonly used. We need to record its form and usage.