Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
    
Many thanks Shawn, I didn't expect the answer to become the selected answer so fast. So: thanks for the selection. – BinaryZebra Dec 14 '15 at 1:36
up vote 1 down vote accepted

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 
share|improve this answer

There isn't a do...while or do...until loop, but the same thing can be accomplished like this:

while true; do
  ...
  condition || break
done

for until:

until false; do
  ...
  condition && break
done
share|improve this answer
    
You can use either of the break lines with either of the loops - while true or until false just mean 'forever'. I understand it kinda preserves the while-ness or until-ness to have the condition happy-exit or sad-exit. – android.weasel Dec 4 '15 at 17:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.