Shell Script While Loop


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:

  1. The condition is evaluated.
  2. If the condition is true (returns a zero exit status), the code block inside the while loop is executed.
  3. After executing the code block, the condition is evaluated again.
  4. If the condition is still true, the code block is executed again.
  5. This process repeats until the 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:

  • The count variable is initialized to 1.
  • The while loop checks whether $count is less than or equal to 5.
  • If the condition is true, it prints the value of count and increments it by 1 using ((count++)).
  • The loop continues until 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.

Example

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:

  1. Start: The script execution begins at the top.

  2. 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.

  3. Loop Execution: If the condition is true, the code inside the loop (between do and done) is executed.

  4. 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.

  5. 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.

  6. Loop Termination: If the condition becomes false, the control moves to the code after the done statement.

  7. 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:

  • Always make sure that the condition in your while loop will eventually evaluate to false, otherwise you will create an infinite loop.
  • Use break and continue statements to control the flow of your while loop.
  • Use variables to track the state of your loop.
  • Test your while loops thoroughly before using them in production.

 

The following control flow diagram shows the execution flow of a while loop in shell scripting:


Enroll Now

  • Shell Programming
  • Machine Learning