Friday, July 26, 2013

SHELL CONSTRUCTS



Shell has the facilities for writing programs. It has the programming constructs to perform logical operations.

Conditions on numbers:
-gt        greater than
-ge       greater than or equal to
-lt         less than
-le         less than or equal to
-eq       equal to
-ne        not equal to

conditions on strings
>          greater than
<          less than
=          equal to
!=         not equal to
-z          zero length
-n         non-zero length

conditions on files:
-r          readable file
-w         writable file
-x         executable file
-s         file exists and not empty
-f          ordinary file
-d         directory
-l          symbolic link


1.if

if          [ condition ]
then
            statements
fi


if          [ condition ]
then
            statements
else
            statements
fi

if          [ condition ]
then
            statements
elif        [ condition ]
then
            statements

fi

2. while:

while     [ condition ]
do
            statements
done

3. until

until      [ condition ]
do
            statements
done

4. for

for variable in list
do
            statements
done

5. arithmetic for:

for (svalue; evalue; incr/decree)
do
            statements
done




6. case

case “value” in

pattern 1 )         statements
                        break;;

patter 2 )           statements
                        break;;


*           )           statements
                        break;;

esac


7. select

select name in list
do
            statements
done


Arrays:

We can arrays using shell. The subscripting will start from 0 to six hundred billion.

Ex:

K=1
While [ $k –lt 10 ]
do
            read val[$k]
            let k++
done

k=1
while [ $k –lt 10 ]
do
            printf “{val[$k]}\n”}
            let k++
done




functions:

function abc {
                        shell commands
                        return (optional)
                        }

A local statement inside a function definition makes the variables involved all become local to that function.

Function abc {
                        local a
                        shell commands
                        }


No comments:

Post a Comment