for, while, until loop, and break out and continue to terminate the loop

for loop

format

for 变量名 in 取值列表
do
  命令序列
done

Insert picture description here

Three different formats of variable i

for i in { } 用法

Insert picture description here

Insert picture description here

for i in $(seq 1 10)

Insert picture description here
Supplement: $(seq 1 2 10)
Insert picture description here

for ((i=1;i<=10;i++))

Insert picture description here
Insert picture description here

Instance

Check the host status according to the IP address. The
IP address is stored in the ipadds.txt file, one per line.
Use the ping command to check the connectivity of each host

Insert picture description here
Insert picture description here

while statement

format

while 条件测试操作
do
    命令序列
done

Insert picture description here

Format example

Insert picture description here
Insert picture description here

Instance

Guess the commodity price game
Obtain a random number through the variable RANDOM.
Prompt the user to guess and record the number of times. After the guess is successful , exit the loop
Insert picture description here
Insert picture description here

until statement

format

until 条件测试操作
do
    命令序列
done

Test a certain condition repeatedly, and execute it repeatedly as long as the condition is not established
Insert picture description here
Insert picture description here
Insert picture description here

Double loop

Insert picture description here
Insert picture description here

break out of the loop

Insert picture description here

Insert picture description here

break out of the two-layer loop

Insert picture description here
Insert picture description here

continue to abort a command in a loop, but not completely abort the entire command

Insert picture description here
Insert picture description here

IFS field separator

Contains spaces, tabs, and newlines by default
Insert picture description here

Instance

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/Jun____________/article/details/114525579