Saturday 7 July 2012

Shell script - 3

Shell script - 3

To go to Session 1 click here
To go to Session 2 click here
To go to UNIX basic commands click here


Starting with “hello”
            The basic things for any program is input and output this can be achived using the two commands
1.      echo which similar to printf in c
2.      read which similar to scanf in c
First script
Example 7
            #!/bash/sh
            echo “hello”
            echo “Enter ur name”
            read nam
            echo “This is my first program $nam”
The Branching Statements
The next important thing in programming is branching to do a specific task based on specific condition.
1.      if .. fi
2.      if..else..fi
3.      if..elif..else..fi
4.      case..esac

if..fi syntax
if [ condition ]
then
            statements
fi
if..else..fi syntax
if [ condition ]
then
            statements
else
            statements
fi
if..elif..fi syntax
if [ condition ]
then
            statements
elif [ condition ]
            statements
else
            statements
fi

in these condition branching statements can be nested or there may be more than one elif based on requirements.
Example 7:
            #!/bin/sh
            filename=$1
if [-r filename] then
echo "file is readable"
elif [-x filename] then
echo "file is executable"
elif [-w filename] then
echo "file is writable"
else
echo "ERRROR: $filename HAS PERMISSION PROB"
fi
            case..esac syntax
            case variable in
                        pattern1)
                                    statements
                        ;;
                        pattern2)
                                    statements
                        ;;
                        ……
                        ……
                        patternn)
                                    statements
                        ;;
            esac
Example 8:
            #!/bin/sh
            name=$1
            case $name in
                        “jaya”)
                                    echo “Welcome buddy”
                                    ;;
                        “devi”)
                                    echo “Welcome sir”
                                    ;;
                        “Shiv”)
                                    echo “Welcome $name”
                                    ;;
            esac
Output
Testhome/l685360# ./test jaya
Welcome buddy
Testhome/l685360#
           
The looping

            The next important thing in programming is looping to do a specific task for some n times loops available in shell script are:

1.      while
2.      for
3.      unitil
4.      select

While Loop:

            Syntax:
                        While [ condition ]
                        do
                                    statements
                        done

Example 9:

#!/bin/sh
echo "Enter n "
read n
i=0
while [ $i -le $n ]
do
        echo "Welcome"
        i=`expr $i+1 `
done
Out Put:
            Testhome/l685360# ./test
Enter n
2
Welcome
Welcome
Welcome
Testhome/l685360#
*note : break can be used to break the loop
*note : continue can be used to continue the looping without executing the rest of lines.
            Example:
                        n=0
                        While [ $n < 10]
                        do
                                    if[ $n == 1]
                                    then
                                                continue;
                                    fi
                                    #if n==1 n becomes true it goes directly go to continue the loop without incrementing because of this, this loop will be endless loop
                                    n=`expr $n+1`
                        done


For loop:

Example 10:
#i!/bin/sh
n1="0 1 2 3 4 5 6 7 8 9 0 10"
for n in $n1
do
        echo $n
        echo "Welcome"
done

Out put:
Testhome/l685360# ./test
0
Welcome
1
Welcome
2
Welcome
3
Welcome
4
Welcome
5
Welcome
6
Welcome
7
Welcome
8
Welcome
9
Welcome
0
Welcome
10
Welcome
Testhome/l685360#

Until loop:

Until syntax:
 until  [ condition ]
 do
            statements
 done

Example 11:

#!/bin/sh
n = 10
x=0
until [ $x –lt $n ]
do
            echo $x
            x = `expr $x + 1`
done


Select Loop:

Select loop syntax:
select variable in word1 word2 …. Wordn
do
            statements
done

Example 12:

#!/bin/sh
select food in breakfast lunch dinner
do
        if [ $food == "breakfast" ]
        then
                echo "Good morning"
        elif [ $food == "lunch" ]
        then
                echo "Good After noon"
        elif [ $food == "dinner" ]
        then
                echo "Good night"
        else
                echo "Invalid option"
        fi
done
                       
Out put:
Testhome/l685360# ./test
1) breakfast
2) lunch
3) dinner
#? 1
Good morning
#? 2
Good After noon
#? 3
Good night
#?
Don't forget to put your feed back as comments which encourages me to do this and you can also post your questions or solve/give answers for others questions, Thanks in advance friends.....



To go to session 4 click here

No comments:

Post a Comment