Sunday, 6 October 2013

Basic Linux Shell Scripting Language : 'While' Loops

while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting, while-loops-in-linux-shell-scripting,

    In the previous article entitled "Basic Linux Shell Scripting Language : Introduction to 'For' Loops", we have observed how a loop works. Loop is nothing but a control flow statement which executes a block of commands repeatedly till certain condition stays true, once the condition becomes false, the loop is terminated.

    In this article, I will explain Basic syntax of 'While' loop along with some examples of 'While' loop usage. If you are new to Shell Scripting, I recommend that, you should read my article - Getting Started - Linux Shell Scripting Language.

Here we go!

The 'While' Loop

The working of while loop in BASH Scripting is similar to that in C Language. There is a block of commands and there is a condition. The block of commands keeps executing till the condition is valid. When condition becomes false, the 'while' loop terminates. Have a look on 'while' loop syntax:

Syntax:


while [ Condition ]
do

    -- Block of Commands --

done

Example:

#!/bin/bash

n=1

while [ $n -le 5 ]
do
  echo "This is Iteration No. $n"
  n=$( expr $n + 1 )
done
Result:

This is Iteration No. 1
This is Iteration No. 2
This is Iteration No. 3
This is Iteration No. 4
This is Iteration No. 5

An Infinite 'While' Loop

You can create an Infinite 'while' loop by writing a condition which is always 'true' or an empty expression ':'.

Example:

#!/bin/bash

while [ 1 ]
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

OR

#!/bin/bash

while :
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

OR

#!/bin/bash

while [ 5 -eq 5 ]
do
   echo "You are in an Infinite Loop. Press CTRL + C to Exit.."
done

'Break'ing the Loop

The break statements are used in the For, While and Until loops to exit from that loop. Conditional break statements are those which exits from the loop upon satisfying a certain condition.

Syntax:

while [ condition ]
do
    -- Some Commands --

    if [ condition ]
    then
        break
    fi

    -- More commands --
done
Example: Lets write a 'while' loop which asks the user to input single-digit numbers and as soon as he enters a number greater than 9, the loop terminates.

#!/bin/bash

while :
do
    echo "Enter a single-digit number :"
    read n

    if [ "$n" -gt 9 ]
    then
        echo "Wrong Entry! Program will terminate now.."
        break
    fi
    
    echo "Good!"
done

The 'continue' Statement

The continue statement in a loop, when certain condition becomes true, skips all the statements coming after it and continues with the next iteration of the loop.

Syntax:

while [ condition ]
do
    -- Some Commands --

    if [ condition ]
    then
        continue
    fi

    -- More commands --
done
Example: Lets print all the even numbers less than or equal to 20.

#!/bin/bash

i=0
while [ $i -le 20 ]
do
    i=$(expr $i + 1)
    j=$(expr $i % 2)

    if [ $j -ne 0 ]
    then
        continue
    fi

    echo "$i"
done

Result:

2
4
6
8
10
12
14
16
18
20

Script : Factorial of a Number

#!bin/bash

echo "Enter the Number"
read num
n=$num
fact=1

while [ $num -ge 1 ]
do
    fact=$(expr $fact \* $num)
    num=$(expr $num - 1)
done

echo "Factorial( $n ) = $fact"
Result:

Enter the Number
6
Factorial( 6 ) = 720

That's all about While Loops!

Also Read: 

0 comments:

Post a Comment

    Total Pageviews