Shell Script for simple calculator to perform addition subtraction multiplication and division based on command line arguments
Program
#!/bin/sh
if test $# = 3
then
case $2 in
+) z=`expr $1 + $3`;;
-) z=`expr $1 - $3`;;
/) z=`expr $1 / $3`;;
x) z=`expr $1 \* $3`;;
*) echo Warning - $2 invalied operator, only +,-,x,/ operator allowed
exit;;
esac
echo Answer is $z
else
printf "Usage - $0\n"
printf "\tvalue1 operator value2\n"
printf "\tWhere, value1 and value2 are numeric values\n"
printf "\toperator can be +, -, /, x (For Multiplication)\n"
fi
Output 1
$ ./calculator-command-line.sh
Usage - ./calculator-command-line.sh
value1 operator value2
Where, value1 and value2 are numeric values
operator can be +, -, /, x (For Multiplication)
Output 2
$ ./calculator-command-line.sh 9 + 12
Answer is 21
Output 3
$ ./calculator-command-line.sh 91 - 34
Answer is 57
Output 4
$ ./calculator-command-line.sh 36 / 4
Answer is 9
Output 5
$ ./calculator-command-line.sh 12 x 5
Answer is 60
Output 6
$ ./calculator-command-line.sh 12 * 5
Usage - ./calculator-command-line.sh
value1 operator value2
Where, value1 and value2 are numeric values
operator can be +, -, /, x (For Multiplication)