Using do – while loop in Linux bash script

April 19, 2021

Similar to the for command, while is also the command for loop operations. The command next to while is evaluated. If it is successful or 0, then the commands inside do and done are executed.

The purpose of a loop is to test a certain condition or expression and execute a given command while the condition is true (the while loop) or until the condition becomes true (the until loop):

while condition

do

commands

done

until condition

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

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

three + 16 =