02. While and until loops while, do, done

while loops

While-loops are much like for-loops, and continue to iterate until the exit status of the condition is false. While-loops consist of the following syntax:

while [ condition exits successfully ]
do
  # Commands here
done

Using shift to iterate through shell arguments

A common use of the while-loop is to iterate through shell arguments, to obtain all option arguments. Each call to the shift command helps us go down the list of options, with the arguments going down one by one. This causes the value of $1 to be replaced by $2, $2 to be replaced by $3, and so on.

Here's a script of how we can combine a while-loop with a case-statement to iterate through all options.

#!/bin/bash
 
while [ $# -gt 0 ]
do 
  case $1 in 
  # File option, followed by filename
  -f) filename=$2
    shift
    ;;
  -v) verbose=true
    quiet=
    ;;
  -q) quiet=true
    verbose=
    ;;
  -l) long=true
    ;;
  # Unrecognized option
  -*) echo "$0: $1: unrecognized option" >&2
    ;;
  **) break
    ;;
  esac
 
  # shift for next option
  shift
done

Getting option parameters with getopts

Another helpful command that is often seen with while-loops is getopts. This command follows standard POSIX option syntax (single letters preceded by a - and possibly one argument value).

For each option that contains an argument, we use the colon (:). For example, we can have a:bc if we plan to have the -a option contain an argument. An example call would look something like:

$ ./test -a argumentValue -b -b

Thus the argumentValue goes with the -a option.

In this example, we design a timer that has several options a user can specify.

#!/bin/bash
 
if [ $# -lt 1 ]
then
  echo "Usage: $0 -t -m -a -r"
  echo "-t time (in seconds)"
  echo "-m message at countdown"
  echo "-a author"
  echo "-r repeat n number of times"
  exit 1
fi
 
# Set default parameters
seconds=5
message="Time's up!"
author="anonymous"
repeatNumber=1
 
# Need to have a colon (:) after each option that has     | an argument.
while getopts t:m:a:r: opt
do
  case $opt in 
    t) seconds=$OPTARG;;
    m) message=$OPTARG;;
    a) author=$OPTARG;;
    r) repeatNumber=$OPTARG;;
  esac
done
 
for iterationNumber in $(seq 1 $repeatNumber) 
do
  echo "Repeating $iterationNumber of $repeatNumber     | times!"
  for sec in $(seq $seconds 1)
  do
    echo "Countdown: $sec"
    sleep 1
  done
done
 
echo "$message by $author"

Aching back from coding all day?

Prism Glasses

Aching back from coding all day? Try Back Problems

Ever feel achy from sitting crunched up on your computer table? Try lying down with these optical glasses that allow you to work on your laptop while lying flat on your back. This is the perfect solution with those with limited mobility or those who wish to prevent neck cramps and back strains.

$ Check price
4.454.45Amazon 4 logo(128+ reviews)

More Back Problems resources

Take your Linux skills to the next level!

Linux for Beginners

Take your Linux skills to the next level! Try Linux & UNIX

Linux for Beginners doesn't make any assumptions about your background or knowledge of Linux. You need no prior knowledge to benefit from this book. You will be guided step by step using a logical and systematic approach. As new concepts, commands, or jargon are encountered they are explained in plain language, making it easy for anyone to understand.

$ Check price
24.9924.99Amazon 4.5 logo(101+ reviews)

More Linux & UNIX resources

Ad