Linux Shell loop statement

The for loop statement is very suitable for occasions where the list objects are irregular and the source of the list is fixed (such as a list file). For situations where it is required to control the number of loops, the operation objects are numbered in numerical order, and repeated operations are performed according to specific conditions, it is more suitable to use another loop - while.
As long as the condition of the loop statement is true, the command will be executed in a loop until the command is not true.

Format of while syntax

The true condition means that the variable is true, and the infinite loop will not stop until an exit or stop command is encountered.

#!/bin/bash
while true
do
   命令
done

Limit the number of loops, let i++, add 1 each time, the number of times starting from zero is less than or equal to 10, if it is greater than 10, stop the loop

#!/bin/bash
i=0
while [ $i -le 10 ]
do
    命令
    let i++
done

while loop case

We already know the format of the loop syntax, the above basic syntax format must be kept in mind, let's see how to use it through a few simple cases.

Case 1: Output 10-1 in descending order

#!/bin/bash
i=10
while [ $i -gt 0 ]
do
        sum=$(( $i + $i ))
        echo $i + $i = $sum
        let i--
done

Case 2: Output the ascending and descending order of 10

[root@daxia sh]# vim while2.sh
#!/bin/bash
a=1
b=10
while [ $a -le 10 ]
do
        sum=$(( $a + $b ))
        echo $a + $b =$sum
        let a++
        let b--
done

Case 3: Create users in batches

We used the for statement to execute it before, this time let's see how while creates users. The batch creation of users here means to write multiple names in a text document, and create users through the document content cycle.

The script for creating a user first considers security issues, and judges whether the current user is an administrator, and if not, does not allow him to use it. There is a set of judgments in the loop. If it is an administrator, it will be created. Sleep means wait for 1 second before recirculating.

#!/bin/bash
while read user
do
        id $user &> /dev/null
        if [ $? -eq 0 ];then
                echo "useradd: $user 你不是管理员 无权创建"
        else
                useradd $user &>> /dev/null
                echo "user: $user 创建成功"
        fi
        echo $user
        sleep 1
done<user.txt

built-in breakout loop

The format is as follows:

exit exits the script, the remaining commands are not executed.
break breaks out of the loop, but executes the commands following the loop.
continue Ends this cycle, but continues to the next cycle, including the commands behind the cycle.

Note: This is a loop, both the for statement and the while statement can be used.

Case 1: exit exits the script

It is also written in the script that I wrote an exit after the first echo, so that the "Hello..." that should have been looped three times only displayed one result, and the third echo was not printed.

#!/bin/bash
for i in {
    
    1..3}
do
        echo "你好,我是大虾好吃吗"
        exit
        echo "这里的打印不出来"
done
echo "这里的也打印不出来"

Case 2: break out of this loop

The usage of break is as follows, which means to exit the loop. If you encounter a break after executing the first echo, you will directly exit the loop command and execute the third echo command. So the first echo command and the third echo command are printed only once during execution.

Case 3: continue ends this loop

The usage of continue is as follows, which means to end the loop and then re-execute the loop statement, and loop according to the value range, until the loop ends, the third echo command is executed.

#!/bin/bash
for i in {
    
    1..3}
do
        echo "你好,我是大虾好吃吗"
        continue
        echo "这里打印不出来"
done
echo "你好,大虾好吃吗"

After learning this, you can write some simple scripts independently, and combine the cases of the previous articles to write a student management system by yourself. I would like to give a suggestion here. The execution script must conform to the identity of the administrator. The student management must have a multi-level directory, and the student information needs to be created, deleted, and modified in the directory. It needs to apply a variety of variables and loop judgment statements, think more, and be good at distinguishing the current level.

Guess you like

Origin blog.csdn.net/qq_40850839/article/details/131699681