Shell_Script

Shell_Script

Shell script for simple addition, subtraction and multiplication using case and function.

####################################################################

#!/bin/sh
#Script depend on three argument. first two are values and last one is operation
#Add Function for each operation
add() {
        result=`expr $1 + $2`;
        echo "Addition of $1 and $2 :- $result";
}

sub() {
        result=`expr $1 – $2`;
        echo "Subtraction of $1 and $2 :- $result";
}

mult() {
        result=`expr $1 \* $2`;
        echo "Multiply of $1 and $2 :- $result";
}

#Checking case with third value.
#Pass two argument $1 and $2 to function
case $3 in
add )
    add $1 $2 ;;
sub )
    sub $1 $2 ;;
mult )
    mult $1 $2 ;;
all )
    add $1 $2 ;
    sub $1 $2 ;
    mult $1 $2 ;;
esac

—————————————————-

Output look like:-

[root@rhel7 Script]# ./cash.sh 2 30 all
Addition of 2 and 30 :- 32
Subtraction of 2 and 30 :- -28
Multiply of 2 and 30 :- 60

####################################################################       

Leave a Reply

Your email address will not be published. Required fields are marked *