Write a shell script to get three arguments from the command line operand 1, operand 3 and perform all arithmetic operations

Write a shell script to get three arguments from the command line operand 1, operand 3 and perform all arithmetic operations

Code:

if [ $# -ne 3 ]
then
        echo "$0:Given argument was wrong.Example sh programname 24 13 + " >&2
        exit 1
fi
echo -----------------------------------------------------------
echo '\tArithmetic Expression using command argument'
echo -----------------------------------------------------------
a=$1
b=$2
op=$3
case $op in
        '+')echo Addition       : $(expr $a + $b);;
        '-')echo Suubtraction   : $(expr $a - $b);;
        '*')echo Multiplication : $(expr $a \* $b);;
        '/')echo Division       : $(expr $a / $b);;
        '%')echo Modules        : $(expr $a % $b);;
        *)echo This is not a choice
esac

Output:

elcot@elcot:~$ sh sum 24 12 / 
----------------------------------------------------------- 
Arithmetic Expression using command argument 
----------------------------------------------------------- 
Division : 2 

Hints:

Example for  run a program to all expression 
elcot@elcot:~$ sh  sum  24  12  +
elcot@elcot:~$ sh  sum  24  12  - 
elcot@elcot:~$ sh  sum  24  12  “*”
elcot@elcot:~$ sh  sum  24  12  / 
elcot@elcot:~$ sh  sum  24  12  %

No comments

Powered by Blogger.