Shell script loop statement

1. for loop

1.1 Execution mechanism: Traversal

Execution mechanism: Assign the elements in the list to the "variable name" in sequence; execute the loop body once after each assignment; until the elements in the list are exhausted, the loop ends. If [in WORDS...] is omitted, the positional parameter variable is used at this
time in “$@”

1.2 Basic format

The first

for 已知循环次数

for  tlj(变量)   循环次数(取值列表)

do
需要循环的事件

done

the second

for (( 表达式1; 表达式2; 表达式3 )); do 命令; done

for ((expr1;expr2;expr3))
do
       command
done

expr1:定义变量并赋初值
expr2:决定是否循环
expr3:决定循环变量如何改变,决定循环什么时候退出

Insert image description here

1.3 Script example

Print a column of question marks

#!/bin/bash
for i  in {
    
    1..9}
do
echo -e " ? "
done

Insert image description here

print a square

#!/bin/bash
for j in {
    
    1..9}
do
for i  in {
    
    1..9}
do
echo -e " * \c"
#\c换行
done
echo
#换行
done

Insert image description here

multiplication table

#!/bin/bash
for j in {
    
    1..9}
    do
       for i in `seq $j`
    do
	 echo -e "${i}x${j}=$[i*j] \t\c"
	 #\t tab键可以对齐
    done
	echo
done

Insert image description here

Method 1 for finding the sum of 1 to 10

#/bin/bash
sum=0
for i in {
    
    1..10}
do
  sum=$[sum+i]
  let i++
done
echo "$sum"

Insert image description here

Method 2 for adding up to 10

#!/bin/bash
sum=0
for i in {
    
    1..100}
do
let sum=$i+$sum
#sum=$[i+sum]
#两种方法都可以
done
echo "$sum"

Insert image description here

Add users in batches

[root@localhost opt]# vim add.sh
#!/bin/bash
ulist=$(cat /opt/user.txt)
for uname in $ulist
do
   useradd $uname
   echo "123123" |passwd --stdin  $uname &>/dev/null
done

Insert image description here
Insert image description here

2. while loop: stop when judged to be false

Compared with for, we need to know the number of loops. We only know the stop condition. If we don’t know the number of times, we need to use while until the condition is reached.

2.1 Basic format

Repeatedly test a certain condition, as long as the condition is true, it will be executed repeatedly, and it will stop when the command is judged to be false

格式:
while  [ 条件测试操作 ]
do						#do代表循环的开始
	判断式/命令序列
done 					#done代表循环的结束

2.2 Self-made mini games

Guess the price game

#!/bin/bash
p=`echo $[RANDOM%1000+1]`
time=0
 
while true
do
let time++
read -p "请输入您猜测的价格(1-1000):" h
if [ $h -eq $p ]
then
echo "恭喜您猜中了,您一共猜测了$time次"
exit
elif [ $h -gt $p ]
then
echo "您猜测的价格过高"
else
echo "您猜测的价格过低"
fi
done

Insert image description here
Insert image description here

Method three for adding up to 10

#!/bin/bash
i=0
sum=0
while [ $i -le 100 ]
do
sum=$[i+sum]
let i++
done
echo $sum

Insert image description here

3. Until loop: Stop when it is judged to be true.

3.1 Basic format

Repeatedly test a certain condition, as long as the condition is not true, it will be executed repeatedly

格式:
until  [ 条件测试操作 ]
do
	判断式/命令序列
done

3.2 Script example

Method 4 for adding up to 10

#!/bin/bash
sum=0
i=0
until [ $i -gt 100 ]
do
 sum=$[sum+i]
 let i++
done
echo "{1..100}的和:$sum"

Insert image description here

4. Nested loops

Using another loop inside a loop is called nested loop
Format:

#!/bin/bash
for ((i=1;i<5;i++))
do
        echo 此${
    
    i}为外部循环
        for((j=1;j<4;j++))
        do
                echo -e "\t此${j}为内部循环"
        done
done

Insert image description here

5. The use of break, exit and continue in the loop statement

5.1Use of break

When the condition is met, break will jump out of the current loop body

#!/bin/bash
for ((i=1;i<5;i++))
do      
        echo 此${
    
    i}为外部循环
        for((j=1;j<4;j++))
        do
            if [ $j -eq 3 ]			<------如果j的值为3
            then
                break				<------跳出当前循环(内部)
            fi  
        echo -e "\t此${j}为内部循环"
        done
done

Insert image description here

5.2Use of exit

When the conditions are met, exit will directly exit the current script

#!/bin/bash
for ((i=1;i<5;i++))
do
        echo 此${
    
    i}为外部循环
        for((j=1;j<4;j++))
        do
            if [ $j -eq 3 ]					<------如果j的值为3
            then
                exit						<------结束当前脚本
            fi
        echo -e "\t此${j}为内部循环"
        done
done
 

Insert image description here

5.3Usage of continue

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

#!/bin/bash
for ((i=1;i<5;i++))
do
        echo 此${
    
    i}为外部循环
        for((j=1;j<4;j++))
        do
            if [ $j -eq 2 ]					
            then
                continue					
            fi
        echo -e "\t此${j}为内部循环"
        done
done

Insert image description here

Guess you like

Origin blog.csdn.net/m0_62231324/article/details/132390702