Linux operating system loop statement - introduction to the use of while statement
This blog will introduce how to use the loop statement-while statement in the Linux operating system. We will detail the grammatical format of the while statement, and provide multiple cases to demonstrate different application scenarios. These use cases include outputting a sequence of numbers, creating users in batches, and using the built-in breakout loop. By learning these contents, you will be able to better grasp the loop control in shell scripting, realize different loop logic and break out of the loop.
1. The grammatical format of while
The syntax of while loop is as follows:
while condition
do
# 执行的代码块
done
When the loop is executed, the value of the condition will be checked first, and if it is true, the operation in the code block will be executed; otherwise, the loop will be jumped out.
2. while loop case
2.1 Case 1: Descending order of output 10-1
Below is an example of using a while loop to output the descending order of 10-1.
num=10
while [ $num -gt 0 ]
do
echo $num
num=$((num-1))
done
The above code will output a sequence of numbers decrementing from 10 until 1 is output.
2.2 Case 2: Ascending and descending order of output 10
We can also output ascending and descending order of sequence of numbers using while loop.
num=1
while [ $num -le 10 ]
do
echo $num
num=$((num+1))
done
num=10
while [ $num -ge 1 ]
do
echo $num
num=$((num-1))
done
The above code will first output the sequence of numbers in ascending order and then in descending order.
2.3 Case 3: Create users in batches
User accounts can be created in batches using a while loop.
count=1
while [ $count -le 5 ]
do
username="user$count"
useradd $username
echo "User $username created."
count=$((count+1))
done
The above code will create user accounts named user1, user2, user3, user4, and user5.
3. Built-in breakout loop
During a loop, we can use the built-in breakout loop statement to terminate the loop early or skip the current loop.
3.1 Case 1: exit script
Execution of the script can be completely exited using the exit statement.
while true
do
read -p "Enter a number (0 to exit): " num
if [ $num -eq 0 ]
then
exit 0
fi
echo "You entered: $num"
done
The above code will ask the user to enter a number, and if the number entered is 0, exit the script through the exit statement.
3.2 Case 2: break out of this loop
Use the break statement to jump out of the current loop and execute the code after the loop.
count=1
while true
do
if [ $count -eq 3 ]
then
break
fi
echo "Count: $count"
count=$((count+1))
done
echo "Loop finished."
The above code will break out of the loop when $count is equal to 3, and then output "Loop finished.".
3.3 Case 3: continue ends this cycle
Use the continue statement to end the current loop and continue the execution of the next loop.
count=1
while [ $count -le 5 ]
do
if [ $count -eq 3 ]
then
count=$((count+1))
continue
fi
echo "Count: $count"
count=$((count+1))
done
The above code will end this cycle when $count is equal to 3, and continue to the next cycle.
in conclusion
This blog introduces how to use the loop statement-while statement in the Linux operating system. We explain the syntax format of the while statement in detail, and provide multiple examples to demonstrate different application scenarios, including the usage of outputting number sequences, creating users in batches, and built-in jumping out of loops. By learning and applying these knowledge, you will be able to better grasp the loop control in shell scripting, realize different loop logic and jump out of the loop. Hope this blog was helpful to you!