Usage of for, while, until loop statement in shell tutorial

loop statement:

There are mainly three loop methods provided in Bash Shell: for, while and until.

1.
For loop The operating method of for loop is to take out the meaning of serial elements, put them into the specified variables in order, and then repeatedly execute the enclosed command area (between do and done) until all elements are exhausted. until.

Among them, serial is a combination of some strings, separated from each other by the separator defined by $IFS (such as a space character), and these strings are called fields.

The grammatical structure of for is as follows:

for 变量 in 串行
do
   执行命令
done

Description:

Line 1, iteratively put the fields in the serial into variables

Lines 2-4, and then repeat the command area between do and done until each field in the serial has been processed.

Flow chart
Insert picture description here
example 1:

Use a for loop to create aaa1-aaa10 in the home directory, and then create a bbb1-bbb10 directory in aaa1-aaa10

#!/bin/bash
for a in {
    
    1..10}
do
        mkdir /datas/aaa$a
        cd /datas/aaa$a
        for b in {
    
    1..10}
        do
                mkdir bbb$b
        done
done
#!/bin/bash
for k in $( seq 1 10 )
do
   mkdir /home/kuangl/aaa${k}
   cd /home/kuangl/aaa${k}
   for l in $( seq 1 10 )
   do
   mkdir bbb${l}
   cd /home/kuangl/aaa${k}
   done
   cd ..
done

Description:

Line 2, seq is used to generate all integers from a certain number to another.

Line 4, create a folder in the home directory.

Line 6, using a for loop to create a folder

Example 2

List the disk space occupied by each subdirectory under the var directory.

#!/bin/bash
DIR="/var"
cd $DIR
for k in $(ls $DIR)
do
  [ -d $k ] && du -sh $k
done

Description:

Line 4, for each file in the /var directory, for loop processing.

Line 6, if the file under /var is a directory, use du -sh to calculate the amount of disk space occupied by the directory.

Second, the while loop
while loop syntax:

1 while 条件测试
2 do
3   执行命令
4 done

Description:

Line 1, the condition test is performed first, if the return value is 0 (the condition test is true), then enter the loop and execute the command area, otherwise

Do not enter the loop, introduce the while command

Line 3, execute the command area. Among these commands, there should be commands to change the condition test, so that there is a chance to

End the while loop after a finite number of steps (unless you want to execute an infinite loop).

Line 4, return to line 1, execute the while command

Flow chart:
Insert picture description here
Example 1

While loop, the classic usage is to read the contents of the file with steering input, as follows:

 #!/bin/bash
while read a
do
        echo $a
done < /datas/6files
1 #!/bin/bash
2 while read kuangl
3 do
4   echo ${kuangl}
5 done < /home/kuangl/scripts/testfile

Description:

Line 2, use read to have standard input to read the data, put it into the variable kuangl, if the read data is not empty, enter the loop.

Line 4, display the changed line data

Line 5, the contents of /home/kuangl/scripts/testfile will be transferred to the input to be read by read.

Example 2

#!/bin/bash
declare -i i=1
declare -i sum=0
while ((i<=10))
do
  let sum+=i
  let i++
done
echo $sum
#!/bin/bash
declare -i i=1
declare -i sum=0
while ((i<=10))
do
  let sum+=i
  let ++i
done
echo $sum

Description:

Line 2-3, declare that i and sum are integers

Line 4, if the condition test: as long as the value of i is less than or equal to 10, execute the loop.

Line 6, sum+=i and sum=sum+i are the same, sum accumulates i.

In line 7, the value of i is incremented by 1. This line is a command to change the condition test. Once i is greater than 10, the loop can be terminated.

Line 8, encounter done, go back to line 6 to perform the conditional test

Line 9 shows that the value of sum is 55

Example 3 while99 multiplication table

#!/bin/bash
a=1
b=1
while ((a <=9))
do
        while ((b<=a))
        do
                let "c=a*b"   #声明变量c
                echo -n  "$a*$b=$c "
                let b++
        done
        let a++
        let b=1  #因为每个乘法表都是1开始乘,所以b要重置
        echo "" #显示到屏幕换行
done

Description:

Line 8, declare variable c

Line 9, echo output display format, -n does not wrap output

Line 13, let b=1 because every multiplication table starts with 1, so b needs to be reset

3. Until loop
The condition test of the while loop is to measure the true value, and the until loop is to measure the false value.

The syntax of until loop:

1 until 条件测试
2 do
3  执行命令
4 done

Explanation:
Line 1, if the conditional test result is false (return value is not 0), enter the loop.

Line 3, execute the command area. Among these commands, there should be a command to change the conditional test, so that you have a chance to end the execution of the until loop after a limited number of steps (unless you want to execute an infinite loop).

Line 4, return to line 1, execute the until command.

Flow chart:
Insert picture description here
Example 1

1 #!/bin/bash
2 declare -i i=10
3 declare -i sum=0
4 until ((i>10))
5 do
6   let sum+=i
7   let ++i
8 done
9 echo $sum

Explanation:
Line 2-3, declare that i and sum are integers

Line 4, if the condition test: as long as the value of i does not exceed 10, enter the loop.

Line 6, sum+=i and sum=sum+i are the same, sum accumulates i.

In line 7, the value of i is incremented by 1. This line is a command to change the condition test. Once i is greater than 10, the loop can be terminated.

Line 8, encounter done, go back to line 6 to perform the conditional test

Line 9 shows that the value of sum is 10

Example 2 until99 multiplication table

#!/bin/bash
a=1
b=1
until ((a>9)) #until 和while相反,条件假的执行,条件真就done结束
do
        until ((b>a))
        do
                let "c=a*b"
                echo -n "$a*$b=$c "
                let b++
        done
        let a++
        let b=1
        echo ""
done

Description:

Line 4, if the condition test: as long as the value of a does not exceed 9, enter the loop, once it exceeds 9, it will not execute, the until and while conditions are opposite, and the condition is true and done.

Line 6, b>a, once b is greater than a, it will not be executed

Guess you like

Origin blog.csdn.net/weixin_46152207/article/details/113876153