Magazine

Using Do – While Loop in Linux Bash Script

Posted on the 19 April 2021 by Satish Kumar @satish_kumar86

Similar to theforcommand,whileis also thecommandfor loop operations. The command next towhileis evaluated. If it is successful or 0, then the commands insidedoanddoneare executed.

The purpose of a loop is to test a certain condition or expression and execute a given commandwhilethe condition is true (thewhileloop) oruntilthe condition becomes true (theuntilloop):

whilecondition

do

commands

done

untilcondition

do

commands

done

The following is the while_01.sh script in which we read a file and display its content:

while_01.sh

#!/bin/bash 
file=/etc/resolv.conf 
while IFS= read -r line    # IFS : inter field separator 
do 
   # echo line is stored in $line 
  echo $line 
done < "$file" 

Let’s test the program:

$ chmod +x while_01.sh
$ ./while_01.sh

The following will be the output after executing the preceding commands:

Output:

nameserver 192.168.168.2
search localdomain

In the following  while_02.sh script, we are printing numbers 1-10 on the screen using the while loop:

while_02.sh

#!/bin/bash 
declare -i x 
x=0 
while [ $x -le 10 ] 
do 
  echo $x 
  x=$((x+1)) 
done 

Let’s test the program:

$ chmod +x while_02.sh
$ ./while_02.sh

The following will be the output after executing the preceding commands:

Output:

012345678910

In the following  while_03.sh script, we ask the user to input the test. If the input of the text is quit, then we terminate the loop; otherwise, we print the text on the screen:

while_03.sh

#!/bin/bash 
INPUT="" 
while [ "$INPUT" != quit ] 
do 
   echo "" 
   echo 'Enter a word (quit to exit) : ' 
   read INPUT 
   echo "You typed : $INPUT" 
done 

Let’s test the program:

$ chmod +x while_03.sh
$ ./while_03.sh

The following will be the output after executing the preceding commands:

Output:

Enter a word (quit to exit) :GANESH
You typed : GANESH
Enter a word (quit to exit) :Naik
You typed : Naik
Enter a word (quit to exit) :quit
You typed : quit

In the following while_04.sh script, we print the content of variable num on screen. We are starting with the value of 1. In the loop, we increment the value of the num variable by 1. When the value of the variable num reaches 6, then the while loop is terminated:

while_04.sh

#!/bin/bash 
num=1 
while (( num < 6 )) 
do 
  echo "The value of num is: $num" 
  (( num = num + 1 ))             # let num=num+1 
done 
echo "Done." 

Let’s test the program:

$ chmod +x while_04.sh
$ ./while_04.sh

The following will be the output after executing the preceding commands:

Output:

The value of num is: 1
The value of num is: 2
The value of num is: 3
The value of num is: 4
The value of num is: 5
Done.

The while_05.sh script prints a series of odd numbers on screen. We are passing a total number of odd numbers required as command-line parameters:

while_05.sh

#!/bin/bash 
count=1 
num=1 
while [ $count -le $1 ] 
do 
  echo $num 
  num=`expr $num + 2` 
  count=`expr $count + 1` 
done 

Let’s test the program:

$ chmod +x while_05.sh
$ ./while_05.sh 5

The following will be the output after executing the preceding commands:

Output:

13579

Back to Featured Articles on Logo Paperblog