Linux While Loop: Bash Scripting Example Guide
In Linux, the usage of while loops is similar to other programming languages. Here is a simple example:
#!/bin/bash
count=1
while [ $count -le 5 ]
do
echo "Count is: $count"
count=$((count+1))
done
In the example above, we used a while loop to print numbers 1 to 5. The loop will continue to execute as long as $count is less than or equal to 5, increasing $count by 1 each time. The current value of $count will be printed in each iteration.
It is important to note that in a while loop, the condition statement needs to be enclosed in square brackets [] and each part should be separated by a space. Additionally, the loop body should be marked by using do and done.