In shell scripting, a while
loop is used to repeatedly execute a block of code as long as a specified condition is true. Here's the basic syntax of a while
loop:
while [ condition ]; do
# Code to execute while the condition is true
done
Here's how it works:
condition
is evaluated.condition
is true (returns a zero exit status), the code block inside the while
loop is executed.condition
is evaluated again.condition
is still true, the code block is executed again.condition
becomes false (returns a non-zero exit status), at which point the while
loop terminates, and the script continues with the next command after the done
statement.Here's an example of a while
loop that counts from 1 to 5:
#!/bin/bash
count=1
while [ $count -le 5 ]; do
echo "Count: $count"
((count++))
done
In this script:
count
variable is initialized to 1.while
loop checks whether $count
is less than or equal to 5.count
and increments it by 1 using ((count++))
.count
is no longer less than or equal to 5.You can also use break
statements within a while
loop to exit the loop prematurely based on certain conditions. Here's an example:
#!/bin/bash
count=1
while true; do
echo "Count: $count"
((count++))
if [ $count -gt 5 ]; then
break # Exit the loop when count is greater than 5
fi
done
In this script, we use while true
to create an infinite loop and then use an if
statement with the break
command to exit the loop when count
becomes greater than 5.
While loops are useful for scenarios where you need to repeat a task until a specific condition is met, such as reading lines from a file, processing user input, or waiting for a specific event to occur.
Here is a simple example which shows that loop terminates as soon as a becomes 6 −
Output:
Creating a control flow diagram for a while loop in a shell script can be challenging due to the limitations of text-based representation. However, I can provide you with a textual description of the control flow for a simple while loop in a shell script.
Here's a description of the control flow for a basic while loop:
Start: The script execution begins at the top.
Condition Check: The condition specified in the while
statement is evaluated. If the condition is initially false, the loop is skipped, and the script proceeds to the code after the done
statement.
Loop Execution: If the condition is true, the code inside the loop (between do
and done
) is executed.
Increment (or Modify Condition): Within the loop, you might have statements that change the variables or conditions used in the loop condition. This step may or may not be present in your specific script.
Back to Condition Check: After executing the code inside the loop, the control goes back to the condition check. If the condition is still true, the loop continues; if it's false, the loop terminates.
Loop Termination: If the condition becomes false, the control moves to the code after the done
statement.
End: The script execution continues with the code after the done
statement.
Here's an example in a more structured text representation:
While loops are a powerful tool that can be used to automate many tasks in shell scripting.
Here are some tips for using while loops in shell scripting:
The following control flow diagram shows the execution flow of a while loop in shell scripting:
Enroll Now