Shell – do…while or do…until in POSIX shell script

control flowshell

There is the well known while condition; do ...; done loop, but is there a do... while style loop that guarantees at least one execution of the block?

Best Answer

A very versatile version of a do ... while has this structure:

while 
      Commands ...
do :; done

An example is:

#i=16
while
      echo "this command is executed at least once $i"
      : ${start=$i}              # capture the starting value of i
      # some other commands      # needed for the loop
      (( ++i < 20 ))             # Place the loop ending test here.
do :; done
echo "Final value of $i///$start"
echo "The loop was executed $(( i - start )) times "

As it is (no value set for i) the loop executes 20 times.
UN-Commenting the line that sets i to 16 i=16, the loop is executed 4 times.
For i=16, i=17, i=18 and i=19.

If i is set to (let's say 26) at the same point (the start), the commands still get executed the first time (until the loop break command is tested).

The test for a while should be truthy (exit status of 0).
The test should be reversed for an until loop, i.e.: be falsy (exit status not 0).

A POSIX version needs several elements changed to work:

i=16
while
       echo "this command is executed at least once $i"
       : ${start=$i}              # capture the starting value of i
       # some other commands      # needed for the loop
       i="$((i+1))"               # increment the variable of the loop.
       [ "$i" -lt 20 ]            # test the limit of the loop.
do :;  done
echo "Final value of $i///$start"
echo "The loop was executed $(( i - start )) times "

./script.sh
this command is executed at least once 16
this command is executed at least once 17
this command is executed at least once 18
this command is executed at least once 19
Final value of 20///16
The loop was executed 4 times