Shell Script to Create a Simple Calculator - The Coding Shala
Home >> Scripting >> simple calculator
Shell Script to Create a Simple Calculator
Write a shell script to create a simple calculator using a switch-case statement?
Code:
#!/bin/bash echo "simple calculator" sum=0 i="y" echo "enter first number" read n1 echo "enter second number" read n2 while [ $i = "y" ] do echo "1.Addition" echo "2.Subtraction" echo "3.Multiplication" echo "4.Division" echo "Enter choice" read ch case $ch in 1)sum=$(echo " $n1 + $n2" | bc -l) echo "Addition is =" $sum;; 2)sum=$(echo "$n1 - $n2" | bc -l) echo "Sub is =" $sum;; 3)sum=$(echo "$n1 * $n2" | bc -l) echo "Mul is =" $sum;; 4)sum=$(echo "$n1 / $n2" | bc -l) echo "div is =" $sum;; *)echo "invalid choice" esac echo "Do you want to continue" read i if [ $i != "y" ] then exit fi
Other Posts You May Like
in the program when we opt to continue its taking the same number as we had given in the starting and it is asking for only operations.....we are not getting the chance to give new inputss
ReplyDeletetake number inputs inside the while loop.
Delete