Bootstrap FreeKB - Bash (Scripting) - while loops
Bash (Scripting) - while loops

Updated:   |  Bash (Scripting) articles

A while loop can be used to iterate through a loop while a certain condition is met. In this example, the value of the $attempt variable will be echoed until the and incremented by one until max attempts (5) is reached.

#!/bin/bash
attempt=0
max_attempts=5

while [ $attempt -le $max_attempts ]; do
  echo "Attempt $attempt of $max_attempts"
  attempt=$(( $attempt + 1 ))
done

 

The following should be returned.

Attempt 0 of 5
Attempt 1 of 5
Attempt 2 of 5
Attempt 3 of 5
Attempt 4 of 5
Attempt 5 of 5

 

Almost always, sleep is used in the while loop to have a brief pause between each attempt.

#!/bin/bash
attempt=0
max_attempts=5
delay=1

while [ $attempt -le $max_attempts ]; do
  echo "Attempt $attempt of $max_attempts"
  attempt=$(( $attempt + 1 ))
  echo "Sleeping for $delay second"
  sleep $delay
done

 

Which should now return the following.

Attempt 0 of 5
Sleeping for 1 second
Attempt 1 of 5
Sleeping for 1 second
Attempt 2 of 5
Sleeping for 1 second
Attempt 3 of 5
Sleeping for 1 second
Attempt 4 of 5
Sleeping for 1 second
Attempt 5 of 5
Sleeping for 1 second

 

As a bit of a more practical example, a while loop is often used along with an if statement and the ps command to determine if a certain process is or is not running. The while loop with sleep 1 make it so that the while loop is only executed for 30 seconds, in this example.

#!/bin/bash
attempt=0
max_attempts=5
delay=1

while [ $attempt -le $max_attempts ]; do
  if [[ `ps -ef` = *bar* ]]; then
    echo "The bar process is running"
    break;
  else
    echo "The bar process is NOT running. Will check again in $delay second"
    attempt=$(( $attempt + 1 ))
  fi
  sleep $delay
done

 

Select in while

One of the most useful features of a while loop is to be able to prompt a user for input, and to then ask the user if they are good, or if they need to be prompted again. To accomplish this, a select loop is nested inside of a while loop.

#!/bin/bash

foo=1
fruit=(banana orange apple grape)

while [ $foo -eq 1 ]; do
    echo "Select your favorite fruit"
    select myFruit in "${fruit[@]}"
    do
        read -rep $'Do you want to select another piece of fruit?\n>' answer
        if [ $answer == "yes" ]
        then
            echo "You selected" $myFruit
        else
            echo "All done"
       
            # When $answer is not yes, we set foo to 0 to end the while loop
            foo=0
        fi

    # When we have nested statements inside of while, we must break out of the loop
    break
    done
done

 

When the above script is run, you will first be prompted to select your favorite fruit.

Select your favorite fruit
1) banana
2) orange
3) apple
4) grape
#

 

After selecting your favorite fruit, you will be asked if you want to select another piece of fruit. If you type yes, you will be asked to select another piece of fruit. If you type anything other than yes, "all done" will be displayed.

Do you want to select another piece of fruit?
>

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter cd4a30 in the box below so that we can be sure you are a human.