Home » Blog » Write a shell script for performing the functions of a basic calculator.

Write a shell script for performing the functions of a basic calculator.

This will execute basic calculator operation.

echo -n "Enter the First Value "
read no1

echo -n "Enter the Second Value "
read no2

echo " *** Choice *** "
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
echo "5. Modulo"

echo -n "Enter Choice: "
read choice

case $choice in
	1) echo -n  $no1 "+" $no2 "is "
	result=`expr $no1 + $no2`
	echo $result;;

	2) echo -n  $no1 "-" $no2 "is "
	result=`expr $no1 - $no2`
	echo $result;;

	3) echo -n  $no1 "*" $no2 "is "
	result=$((no1 * no2))
	echo $result;;

	4)  echo -n  $no1 "/" $no2 "is "
	echo "scale=3;$no1 / $no2"|bc
	echo $result;;

	5) echo -n $no1 "%" $no2 "is "
	echo "scale=3;$no1 % $no2"|bc;;

	*) echo "Invalid Choice"
esac

 

Leave a Reply

Your email address will not be published.