loop -untill

Bash Until Loop in a bash scripting is used to execute a set of commands repeatedly based on the boolean result of an expression. 

The set of commands are executed only until the expression evaluates to true. 
It means that when the expression evaluates to false, a set of commands are executed iteratively. 
The loop is terminated as soon as the expression evaluates to true for the first time.


until [ expression ];  
do  
command1  
command2  
. . .  
. . . .   
commandN  
done  


The condition is checked before executing the commands.
The commands are only executed if the condition evaluates to false.
The loop is terminated as soon as the condition evaluates to true.
The program control is transferred to the command that follows the 'done' keyword after the termination.

The while loop vs. the until loop
The 'until loop' commands execute until a non-zero status is returned.
The 'while loop' commands execute until a zero status is returned.
The until loop contains property to be executed at least once.


-------------------------------------------------------------------------------------------------------------------
Example
-------------------------------------------------------------------------------------------------------------------

#! bin/bash
# Untill Loops

number=1
until [ $number -ge 5 ]
do
echo $number
number=$(( number+1 ))
done


[prabhucloudxlab@cxln4 ~]$ sh hello.sh
1
2
3
4


:'
Note:
Until loop is used to execute a block of code until the expression is evaluated to be false.
While loop runs the code block while the expression is true and until loop does the opposite.


---------------------------------------------------------------------------------------------------------------------------------
Single Condition
---------------------------------------------------------------------------------------------------------------------------------
-- Script
[haritaraka12078@cxln4 ~]$ cat>until1.sh

#!/bin/bash
#Bash Until Loop example with a single condition
i=1
until [ $i -gt 10 ]
do
echo $i
((i++))
done



[haritaraka12078@cxln4 ~]$ sh until1.sh
1
2
3
4
5
6
7
8
9
10



---------------------------------------------------------------------------------------------------------------------------------
Multiple Conditions
---------------------------------------------------------------------------------------------------------------------------------

-- Script
[haritaraka12078@cxln4 ~]$ cat>until2.sh

#!/bin/bash
#Bash Until Loop example with multiple conditions
max=5
a=1
b=0
until [[ $a -gt $max || $b -gt $max ]];
do
echo "a = $a & b = $b."
((a++))
((b++))
done



[haritaraka12078@cxln4 ~]$ sh until2.sh
a = 1 & b = 0.
a = 2 & b = 1.
a = 3 & b = 2.
a = 4 & b = 3.
a = 5 & b = 4.


 

'