Shell Script for simple calculator to perform addition subtraction multiplication and division based on the symbol using case statements
Program
echo "Enter the first number"
read a
echo "Enter the second number"
read b
echo "Enter the operator:"
echo -e "Addition: +\nSubtraction: -\nMultiplication: x\nDivision: /"
read op
case $op in
+) c=`expr $a + $b`
echo "Sum of $a and $b is $c";;
-) c=`expr $a - $b`
echo "Difference of $a and $b is $c";;
x) c=`expr $a \* $b`
echo "Product of $a and $b is $c";;
/) c=`expr $a / $b`
echo "Division of $a and $b is $c";;
*) echo "Invalid Operator"
exit;;
esac
Output 1
$ chmod 755 calculator-using-case.sh
$ sh calculator-using-case.sh
Enter the first number
7
Enter the second number
9
Enter the operator:
Addition: +
Subtraction: -
Multiplication: x
Division: /
+
Sum of 7 and 9 is 16
Output 2
$ sh calculator-using-case.sh
Enter the first number
6
Enter the second number
12
Enter the operator:
Addition: +
Subtraction: -
Multiplication: x
Division: /
-
Difference of 6 and 12 is -6
Output 3
$ sh calculator-using-case.sh
Enter the first number
5
Enter the second number
3
Enter the operator:
Addition: +
Subtraction: -
Multiplication: x
Division: /
x
Product of 5 and 3 is 15
Output 4
$ sh calculator-using-case.sh
Enter the first number
99
Enter the second number
3
Enter the operator:
Addition: +
Subtraction: -
Multiplication: x
Division: /
/
Division of 99 and 3 is 33
Output 5
$ sh calculator-using-case.sh
Enter the first number
6
Enter the second number
7
Enter the operator:
Addition: +
Subtraction: -
Multiplication: x
Division: /
%
Invalid Operator