In the previous section , we learned about branch statements in flow control statements. In this section, let's take a look at the last part of flow control-loop statements, and learn to use three common loop statements (for while until).
Introduction to loop statements
Loop statement refers to a statement that can be executed repeatedly under certain conditions. Mainly in order to meet certain circumstances to perform some repetitive but slightly different actions, such as batch modification of files, timing execution of commands, anti-copying Trojans, etc. With the loop statement, the computer program truly embodies its computational advantages, and greatly improves the coding efficiency of the program.
for loop
Introduction
The for loop is a fixed loop. The for loop provides a loop for the statement and at the same time makes the variable enumerate and iterate. Every time a variable gets a new enumerated or iterated value in a range or set, loop the statement in the loop body once
grammar
#第一种形式:枚举
for Var in Key1 Key2 Key3...
#其中,Var为变量 Key1 Key2等为变量枚举中选中的集合值
do
<Action>
done
#第二种形式:迭代
for Var in {
n1..n2[..step]}
#n1 为起始数字;n2 为结束数字; step可选,为步长,即每步增加的数字
do
<Action>
done
Example
#以下示例求1到100中所有奇数的和
sum=0
for i in {
1..100..2}
do
sum=$[$sum + $i]
done
echo $sum
Precautions
- For the iterative form, the starting data of the data is not only numbers, but also letters, etc. For example,
{a..z}
all lowercase letters will be iterated again - Enumerations do not require parentheses, just need a space between each value
- The value of the loop variable can be changed in the loop body , but it will not affect the loop process. Enumeration is the same as iteration. After entering the next loop, regardless of the current variable value, it will iterate to the next value
C language style for loop
The for loop of bash also supports C language style, the syntax is as follows, other usage is the same. For details, please refer to the C language for loop
for ((i=1;i<=10;i++))
do
echo $i
done
- Note that the C-style for loop in bash contains two parentheses
- The value of the loop variable can be changed during the running process , which is limited to the C language style loop
while loop
Introduction
The while loop is different from the for loop. The while loop is an indefinite loop, also called a conditional loop. There is no loop variable to limit the number of times. When the loop condition is met, the loop will continue until the loop condition is not met.
grammar
while [Condition]
do
<action>
done
Example
#以下示例同样求1-100中所有奇数的和
#!/bin/bash
#Author:Zheng
i=1
sum=0
while [ $i -lt 100 ]
do
sum=$[$sum + $i]
i=$[$i + 2]
done
echo $sum
Precautions
-
Note the spaces inside the brackets
-
If you want to achieve an endless loop, you can add a constant variable in the conditional expression, and you only need to set the condition in the loop body to make the loop jump out (see below for details)
-
The loop must set a valid exit condition (in the loop condition or the loop body), otherwise it may cause resource occupation and system crash
The following example uses an infinite loop and uses the break statement to jump out:
i=0
while [ 1 -eq 1 ]
do
i=$[$i + 1]
echo $i
if [ $i -ge 10 ]; then
break
fi
done
until loop
Introduction
Both until loop and while loop are indefinite loops, but their conditional judgment method is opposite to while. As the name implies, the until loop statement will continue to loop as long as the conditional judgment is not established, and jump out of the loop until the conditional judgment is established. Part of the content is the same as the while loop, so I won’t repeat it
grammar
The until loop syntax in Bash is different from some programming languages. The until conditional judgment sentence is before the loop body. If the condition is met at the beginning, the loop is skipped directly instead of executing the loop body first and then judging to jump out.
until [Condition]
do
<Action>
done
Example
#本示例同样完成1-100中所有偶数的和
#!/bin/bash
#Author:Zheng
i=1
s=0
until [ $i -gt 100 ]
do
s=$[$s + $i]
i=$[$i + 2]
done
echo $s
Precautions
- Pay attention to the spaces inside the brackets in the conditional judgment
Skip and jump out of the loop
Although there is only one word difference between skipping and jumping out of the loop, the concept is completely different
grammar
Mainly use continue
statements and break
statements
for i in {
1..100}
do
<action>
continue #仅跳过当前循环进入下一循环
continue n #n为数字,跳过n层循环
done
Example
#此示例在i为偶数时跳出,偶数循环号下不会显示"Finish"
for i in {
1..20}
do
echo $i
if [ $[$i % 2] == 0 ]; then
continue
#当前仅有1层循环,continue 2 表示直接跳过当前的第二层循环,进入第二层的下一迭代
fi
echo "Finish"
done
echo "Final"
#此示例在i为10时退出,不输出Finish而直接Final
for i in {
1..20}
do
echo $i
if [ $i == 10 ]; then
break
#当前仅有1层循环,break 2 表示直接跳出当前两层循环
fi
echo "Finish"
done
echo "Final"
Precautions
- The skip of the loop is generally used in conjunction with the conditional judgment statement to increase the flexibility of the loop
continue
In the andbreak
command, the number added after it represents the number of cycles , not the number of cycles . Some references may be wrong, please note
to sum up
The three loop formats can be used in common with each other, and a loop will be more suitable in different environments. Please choose the loop statement you need to use based on the actual scenario.
In this section, we officially ended the learning of Shell programming, more in-depth functions and so on. Since Bash is not an object-oriented programming language, it is not widely used in daily life. I will not cover it temporarily. I will update it later if necessary.
The previous section, Linux Bash Shell Programming (9): Flow Control Statement (Part 1) Branch Statement (if, case)