Looping statements
The looping statements in the shell are as follows:
for
while
unitl
case
select
For/while/unitl must end with done
forCycle
forLoops are used to iterate through an array (data list) or execute a calculation statement.
forthere are two forms of looping in the shell
The first data to traverse a word sequence word can be a space-delimited word, an array variable, or a numeric range/letter range.
Example:
The number of cycles is the number of data series, each time the variable is i assigned to the current loop to the data, you can use in the loop body to access the variable i.
The second, example:
In the above cycle, expr1 for i=0 ; expr2 for i<10 ; expr3 For i++ , which expr1 executes before the loop starts, expr2 defines the condition that runs the loop, and expr3 executes in the loop.
whileCycle
The while loop executes a code block when the specified condition is true.
Grammar:
' List-1 可以是命令,表达式,如果命令或表达式一直为真则继续执行 list-2 ' until it does not stop the loop.
Example:
If the command LS executes successfully, the command echo, and LS are executed until the command execution fails.
Example 2, using an expression:
In the above example, first defines a variable i assignment value of 0, and then use the expression to determine whether the value of I is less than 10, if less than 10 executes the command in the while loop body, let i++ each time I plus 1, when i=10 I is not less than 10, the loop ends.
untilCycle
untilLoops are while variants of loops. The loop executes the block of code once before checking whether the condition is true, and then exits the loop if the condition is true.
Grammar:
Example:
When the command execution succeeds, the loop ls ends, the until loop first determines ls whether the execution succeeds, and if it succeeds, does not continue the loop until the command executes successfully.
whileas with loops, until you can also judge an expression and jump out of the loop if the expression succeeds.
caseStatement
caseA statement is a pattern-matching statement that performs different actions based on different conditions, with the following syntax:
It will word pattern run the command in that mode with an attempt to match and if it matches.
Example:
In the example above, the first parameter passed in cash.sh is matched, and the command in each mode must ;; end, and the case statement must esac end. *for the default match, if the previous mode does not match, run the command in that mode, and if you do not want to execute the command in one mode, you can enter two semicolons to ;; end.
selectStatement
selectStatement can print a list of options for interacting with the user.
Grammar:
selectA variable is defined and then assigned to the value of the name word word corresponding index in the data series that corresponds to the number selected by the user name .
In the following sake, I will selelct use it together with the case statement.
When executing the test.sh script, ... Select 将序列数据打印处理供用户选择,而 Select 只接受数字序列,然后 Case 语句将用户选择的数据做匹配,做出相应的动作。<br/>当用户选择1时, Select 将其序列数据中的位置1的值赋值给变量 I case ,传递到 ' statement, and then make the corresponding action.
breakAnd
continueStatement
breakAnd continue can jump out of the loop. The difference is that it jumps out of the loop and ignores the loop break continue as it resumes the download cycle. They can be for used in, while and in until loops.
breakStatement
In the above sake, when it is i equal to 2 o'clock, when 2 % 2 == 0 , it jumps out of the loop, and the following loop will not continue.
continueStatement
At the top of the sake, when i % 2 == 0 the continue statement terminates when the secondary loop continues, the next loop is executed, thus outputting the odd number in 1-10.
Linux Shell Programming (eight): Looping structure