How to use Until loop in shell script

How to use Until loop in shell script


The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.



Syntax:-

until command
do
   Statement(s) to be executed until command is true
done





Here the Shell command is evaluated. If the resulting value is false, given statement(s) are executed. If the command is true then no statement will be executed and the program jumps to the next line after the done statement.

#! /bin/bash

until loops (if condition is false the until loops work)

until [ condition ]
do
     command1
     command2
     command3
done


Example:

n=1
until [ $n -le 6 ]
do
      echo $n
      n=$(( n+1 ))
done

Example:

n=1
until [ $n -ge 6 ]
do
         echo $n
         n=$(( n+1 ))
done

Share:

0 comments

Please leave your comments...... Thanks