Until in Shell Script


Until


until is very similar to the while loop, but it is totally reverse of while condition. In this, the loop continues until the condition becomes true, not while the condition is true.

Syntax:


until condition
do
statements
done






Difference in while and until


While Loop executes the block of code (enclosed in do...done) when the condition is true and keeps executing that till the condition becomes false. Once the condition becomes false, the while loop is terminated.

Until Loop executes the block of code (enclosed in do...done) when the condition is false and keep executing that till the condition becomes true. Once the condition becomes true, the until loop is terminated.


Example:


#!/bin/bash 
a=20
until [ $a -lt 10 ]; do
    echo $a
    let a-=1
done


Here the value of a has initialize to 20, At first iteration it prints the digits from 20 then a=a-1 decreases the value and again loop executes and prints a equal to 19. This process continues till value of a goes to 10. In next iteration a becomes 9 and then the until condition becomes true and the loop terminates. 


shell-scripting-while-loop

No comments: