Belajar Bash: Until Loop

The until loop will continue until a specific requirement is met or  as long as the exit status is NOT zero.  This is the opposite of the while loop.

In the example below you can see that the variable “i” starts with 0 and will loop until it equals 100.  It is incremented by one each loop.  When it reaches 100 it will deliver the zero as an indication of success and the loop will stop.

#!/bin/bash
i=0
until [ $i -eq 100 ]
do
echo $i
i=$(( $i +1 ))
done

Here is the opposite script counting down from 200.  This script counts down until the number is less than 9.  Note that let is used with a negative number.

#!/bin/bash
COUNTER=200
until [  $COUNTER -lt 9 ];
do
echo COUNTER $COUNTER
let COUNTER-=1
done

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: