Shell Commands

1. echo - to print the result

2. read - to get the input from the user

3. $s - access the value of s

4. -ge - greater than or equal

-le - less than or equal

-gt - greater than

-lt - less than

-eq - not equal

5. # - command line

6. + - addition

- - subtraction

\* - multiplication

/ - division

7. a=`expr $a + $b` - perform any arithmetic operation

8. case $a in - switch statement

1) ...... ;;

2) ...... ;;

3) ...... ;;

4) ...... ;;

esac

9. for((i=1;i<=5;i++))

do

.....

done

10. while [ ]

do

.....

done

11. if [ ]

then

.....

elif [ ]

then

.....

else

.....

fi

12. –a - logical AND

-o - logical OR

! - NOT

13. save the file without any extension

sh filename for compilation

./a.out to view the output

Example Programs

1. Program for arithmetic operation

echo enter the value a

read a

echo enter the value b

read b

c=`expr $a \* $b`

echo $c

2. Program for simple calculator

echo a=

read a

echo b=

read b

echo 1.add

echo 2.subtract

echo 3.multiply

echo 4.divide

echo enter the choice

read ch

case $ch in

1) c=`expr $a + $b`

echo result is c;;

2) c=`expr $a - $b`

echo result is $c;;

3) c=`expr $a * $b`

echo result is $c;;

4) c=`expr $a / $b`

echo result is $c;;

esac

3. Program for if..else statement

echo a=

read a

echo b=

read b

echo c=

read c

if [ $a -ge $b -a $a -ge $c ]

then

echo a is big

elif [ $b -ge $a -a $b -ge $c ]

then

echo b is big

else

echo c is big

fi

4. Program using for loop

echo "enter the value of n"

read n

for ((i=1 ; i<=$n ; i++))

do

echo $i

done

5. Program for while statement

echo enter the value of n

read n

s=0

while [ $n -gt 0 ]

do

s=`expr $s + $n`

n=`expr $n - 1`

done

echo the result is $s