10-Bash Scripting – While loop & Until loop commands

Hello Linux-fanatics, In our last tutorial we learned to use READ command to take user’s Input & also learned to redirect output of a script to a file. In this tutorial we will be discussing WHILE & UNTIL loop commands.

WHILE loop command

While loop allows us to define a command/condition & then it keeps the condition in loop until loop returns with a non-zero exit status.

Syntax for using while loop

while test-command
do
other-commnds
done

Conditions/parameters used with while command are same as used with if-then statements.

Example script

#!/bin/bash
#checking while loop
num=0
while [ $num –lt 10 ]
do
echo $num
num1=$[$num1+1]
done

This script keeps on printing value for $num1 up until value is less than 10 i.e. upto 9. As soon as it reaches to 10 loop completes & will stop.

NOTE:- Very important thing to consider when using loops is that you are not stuck in infinite loops. Change   num1=$[$num1+1] to num1=$[$num1-1] & you will understand what I mean. If you are stuck in one such loop, press ‘ctrl+C’ to get out of it & always be cautious when using loops.

 

Until Command

Until command is just opposite of while command. Until loop runs until it has exit status of zero.

Syntax for until command is same as while command i.e.

until test-command
do
other-command
done

Example script

#!/bin/bash
# checking until command
num=10
until [ $num –eq o ]
do
echo $num
num=$[$num-1]
done

 

Nested loops

We can use one loop inside the other loop, if needed. First loop used is called outer loop & the loop inside the outer loop is called inner loop.

Example script

#!/bin/bash
# checking nested loop
num1=10
while [ num1 –lt 20 ]
do
echo “outer loop = $num1”
for (( num2 = 1; $num2 > 5; num2++ ))
do
num3= $[ $num1*$num2]
echo “Inner loop : $num1 * $num2 = $num3”
done
num1=$[$num1+1]
done

Reading data from a file

We can also read data from a file as input for variables in the script. Let’s create a script doing just that,

Create a file named “hobbies.txt” in /home/dan with following content

Dan: loves football

Susan: loves table-tennis

Daniel: loves sleeping

Now, we will write a script to read the content of this file as input values for our variables

#!/bin/bash
# Reading data from file
IFS.OLD=$IFS
IFS=$’/n’
for hobbies in $(cat /home/dan/hobbies.txt)
do
echo “Data in $hobbies”
IFS=:
for data in $hobbies
do
echo “ $ hobbies”
done
done

Let’s elaborate what the script means,

IFS.OLD=$IFS  is used when & if we need to restore old values of IFS (Internal field seperator)

Next, IFS=$’’\n’ means , now only new line will be a separator

Then, we declared ”hobbies” as variables which will take data from text file,

And then we printed values of our variable “hobbies” in outer loop & then with “:” as separator , we printed “hobbies” values one after another in separate lines.

 

This ends our tutorial. Next up, we will discuss “how we can control our loops by using break & continue commands

Also, mention you r queries/comments down below.ADIOS!!!

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter – Make a contribution via PayPal

[paypal_donation_button align=”left” border=”1″]

Linux TechLab is thankful for your continued support.

Shusain

Passionate about Linux & open source. Loves to learn, read & write about Linux as well as new technologies.

You may also like...

Leave a Reply