Shell-programming loop statement

Loop statement function (meaning)

In actual work, we often encounter situations where a certain task needs to be executed multiple times, and each time it is executed, only the processed objects are different, and other commands are the same. For example, create a system account based on the name list in the address book, check the survival status of each host based on the server list, set a firewall policy that denies access based on the IP address blacklist, and so on.
When faced with various list repetitive tasks, it is difficult to use simple if statements to meet the requirements, and it is extremely cumbersome and difficult to write all the code sequentially. Loop statements can solve similar problems well.

One, for loop

1. For statement structure

Read different variable values ​​to execute the same group of commands one by one

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

Insert picture description here

2. Examples

①Calculate the sum of all integers from 1 to 100

a=0
for (( i>=1;i<=100;i++))
do
 a=$[$i+$a]
done
 echo $a

Insert picture description here

Insert picture description here
② Use for loop to make 99 multiplication table

#!/bin/bash
#for循环99乘法表
for (( a=1;a<=9;a++ ))
do
 for (( b=1;b<=$a;b++ ))
 do
 c=$[$a*$b]
 echo -n -e "$b*$a=$c\t"
 done
 echo ""
done

Insert picture description here
Insert picture description here

Two, while loop

As long as the condition is established, the loop will be repeated, and it will stop immediately if it fails.

1. While statement structure

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

Insert picture description here

2. Examples

①Calculate the sum of all integers from 1 to 100

#!/bin/bash

a=0
i=1
while [ $i -le 100 ]
do
 a=$[$i + $a]
 let i++
done
 echo $a                                                                                                                   

Insert picture description here
Insert picture description here
②Use while loop to make 99 multiplication table

#!/bin/bash
#while循环99乘法表
a=0
while [ $a -lt 9 ]
do
let a++
 b=0
 while [ $b -lt $a ]
 do
 let b++
 c=$[$a*$b]
 echo -n -e "$b*$a=$c\t"
 done
 echo ""
done

Insert picture description here
Insert picture description here

Three, until loop

Repeatedly test a certain condition, as long as the condition is not established, the loop will be repeated

1. Until statement structure

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

Insert picture description here

Instance

Calculate the sum of all integers from 1 to 100

#!/bin/bash

a=0
i=1
until [ $i -gt 100 ]
do
 a=$[$i + $a]
 let i++
done
 echo $a

Insert picture description here
Insert picture description here

4. Example: Guess the size game

Enter a value to guess the size

#!/bin/bash

price=$[$RANDOM % 1000]
a=0
b=0
echo "猜猜商品价格是多少"
while [ $a = 0 ]
do
let b++
read -p "请输入你猜的价格:" c
if [ $c = $price ];then
 echo "恭喜你猜对了!"
 echo "你总共猜了 $b 次"
 let a++

elif [ $c -lt $price ];then
 echo "猜小了,重新输入价格!"

elif [ $c -gt $price ];then
 echo "猜大了,重新输入价格!"
fi

done

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/s15212790607/article/details/114525316