Friday, 24 June 2016

Shell Script Programs For Beginners - SET 02 ( Case and Loop )

1). Switch case Example 01
2). Switch case Example 02
3). Switch case Example 03
4). For loop Example01 : Simple for loop.
5). For Loop Example02 : List all content of current directory.
6). For loop Example03: Executing unix command in loop.
7). While loop Example 01: simple while loop
8). Until loop Example01: Simple until loop
9). For loop with break keyword
10). For loop with continue keyword

1) Switch case Example 01

#!/bin/bash
#
#Script Name : caseExample01.sh 
#Description : Script will read a value from user and choose accordingly choice.
#
echo "Enter the number from 1 to 4"

read x

case $x in 
    1) echo "One";;
    2) echo "Two";;
    3) echo "Three";;
    4) echo "Four";;
    *) echo "Wrong input, Please enter number 1 to 4";;
esac

Output:
Case 1 : if x=2
Two
Case 2 : if x=4
Four
Case 3 : if x=0 or 5 or 10 or d
Wrong input, Please enter number 1 to 4


2) Switch case Example02
#!/bin/bash
#
#Script Name : caseExample02.sh 
#Description : Script will read a value from user and choose accordingly choice.
#               Keeping default '*)' in between rather keeping it at end and observe the output of program.
#

echo "Enter the number from 1 to 4"
read x
case $x in 
    1) echo "One";;
    2) echo "Two";;
    3) echo "Three";;
    *) echo "Wrong input, Please enter number 1 to 4";;
    4) echo "Four";;
esac

Output:
Case 1 : if x=1
One
Case 2 : if x=4
Wrong input, Please enter number 1 to 4
Case 3 : if x=0 or 5 or 10 or d
Wrong input, Please enter number 1 to 4


If you observe the above program it is clear that the case which are places after default case those does not come under consideration of program.

3) Switch case Example 03
#!/bin/bash
#
#Script Name : caseExample03.sh 
#Description : Script will read a value from user and choose accordingly choice.
#               We can use expression also in case.
#

case `expr 20 + 5` in 
    10) echo "Ten";;
    25) echo "Twenty Five";;
    30) echo "Thirty";;
    *) echo "Wrong input";;
esac

Output:
Twenty Five

Loop:
Loops are very powerful tool to perform a set of instructions repeatedly until a certain condition is reached.
For,While and Until are the popular loop in shell program . Following are the example of each kind of loops.

4) For loop Example01 : Simple for loop
#!/bin/bash
#
#Script Name : for01.sh
#Description : Script showing demo of a simple for loop
#

for val in a b c d e f g h i j
do
        echo $val
done


5) For Loop Example02 : List all content of current directory
#!/bin/sh
#
#Script Name : for02.sh
#Description : Script showing demo of a for loop in which program listing all the contents of current directory.

for val in *
do
  echo "$val"
done

Remember * is Wildcard which will consider all the contents of current directory hence the above program will loop all the file and directory available in current directory.

6) For loop Example03: Executing unix command in loop
#!/bin/sh
#
#Script Name : for03.sh
#Description : Script showing demo of a for loop in which program is executing unix commands

for val in date cal exit who

do
   "$val"
done

Output:
Mon Dec 12 22:05:57 IST 2016
   December 2016
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

Note : Replcae "$val" to echo "$val" in above program and observe the output.


While Loop:

In 'while' loop, we give one control command to control the iteration of the loop
A set of commands will be executed as long as the control command's exit status is 0
When the control command's exit status becomes 1, the 'while' loop is quit.

7) While loop Example 01: simple while loop
#!/bin/sh
#
#Script Name : while01.sh
#Description : A simple while loop demo

a=0

while [ $a -lt 10 ]
do
   echo -ne "$a\t"
   a=`expr $a + 1`
done


Output:
0       1       2       3       4       5       6       7       8       9

Note : In above program i used -n and -e option where -n option is used to disable trailing new line character.
and -e option is used to enable escape sequences and \t is used for tab.

Until loop:
A until loop is used to repeat set of commands as long as the control command's exit status is 1
When the control command's exit status becomes 0, the loop is get quit.

8) Until loop Example01: Simple until loop
#!/bin/sh
#
#Script Name : until01.sh
#Description : A simple until loop demo


echo  "Type 'end' to exit  "
read var
until  [  "$var"  =  "end"  ]
do
    `echo $var`
     read  var
done

echo  "End of the script and exiting"


Output:
Type 'end' to exit
date
Mon Dec 12 23:13:00 IST 2016
end
End of the script and exiting


Break and Continue Keywords:
In shell scripts we can use break and continue keywords only in the looping statements (i.e., while , until and for).

9) For loop with break keyword
In the following example as soon variable i become equal to 5 the loop breaks and quit.
#!/bin/sh
#
#Script Name : break.sh
#Description : A simple break keyword in a loop


for i in 1 2 3 4 5 6 7 8
do
    echo $i
    if [ $i -eq 5 ]
    then
        break
    fi

done

Output:
1 2 3 4 5

10) For loop with <b>continue</b> keyword
Following example showing the a demo of 'continue' keyword in which it print which are not equal to 5, as soon controller reach to 5 continue keyword move it to the next number.

#!/bin/sh
#
#Script Name : continue.sh
#Description : A simple continue keyword in a loop

for i in 1 2 3 4 5 6 7 8

do
#   echo $i
    if [ $i -ne 5 ]
    then
        echo $i
    else
        continue
    fi

done


Output:
1 2 3 4 6 7 8

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...