How to make your own calculator using shell script in Linux

Shell script is the powerhouse of Linux. We have done various things in shell scripting up to now. If you are reaching this tutorial directly and you don’t have a single clue about the shell script I recommend looking at our shell scripting section first. In this tutorial we are going to build a simple calculator which will perform the simple mathematical calculations: addition, subtraction, multiplication and division. For this, we are going to use the select statement, case statement and expr command. Select statement will help us create the menu.  Remember select only runs in Korn(ksh) shell. Case statement will help us create the structure and expr command will help us to perform mathematical calculations.

You should know the following things to make the calculator using shell script:

Creating Menu:

First, let us create the menu using the select statement.

echo "------------"
echo "Calculator"
echo "------------"
PS3="Press 1 for Addition, 2 for subtraction, 3 for multiplication and 4 for division: "
select math in Addition Subtraction Multiplication Division
do
   break
done

This will create the menu and ask the user if they want to perform addition, subtraction, multiplication or division. See the output.

Calculator menu

Now we have to add the block of code between do and done using case statement which will create a full calculator.

Full Calculator:

We created the menu and now we will do the addition, subtraction, multiplication and division. After the user do what they desire, they will be asked if they want to calculate again or not. If they want to, it will start from the beginning. For this, we will use the while loop.

input="yes"
while [[ $input = "yes" ]]
do
	echo "------------"
	echo "Calculator"
	echo "------------"
	PS3="Press 1 for Addition, 2 for subtraction, 3 for multiplication and 4 for division: "
	select math in Addition Subtraction Multiplication Division
	do
		case "$math" in
		Addition)
			echo "Enter first no:"
			read num1
			echo "Enter second no:"
			read num2
			result=`expr $num1 + $num2`
			echo Answer: $result
			break
		;;
		Subtraction)
			echo "Enter first no:"
			read num1
			echo "Enter second no:"
			read num2
			result=`expr $num1 - $num2`
			echo Answer: $result
			break
		;;
		Multiplication)
			echo "Enter first no:"
			read num1
			echo "Enter second no:"
			read num2
			result=`expr $num1 * $num2`
			echo Answer: $result
			break
		;;
		Division)
			echo "Enter first no:"
			read num1
			echo "Enter second no:"
			read num2
			result=$(expr "scale=2; $num1/$num2" | bc)
			echo Answer = $result
			break
		;;
		*)
			echo Choose 1 to 4 only!!!!
			break
		;;
	esac
	done
	echo "Do you want to calculate again(yes/no):"
	read input
	echo "Thank you for using this program"
done

We have used the while loop, created the menu, perform some mathematical calculations. In the multiplication code:

result=`expr $num1 * $num2`

The backslash(‘’) is used because * is the keyword in shell script. The shell will not understand * as the multiplication sign. Thus, to let know the shell script, we want multiplication out of * we used backslash.
And in the division code.

result=$(expr "scale=2; $num1/$num2" | bc)
See the thing is shell script ignore the decimal while doing the division. For example: If we divide 10 by 8 it will show 1 as the result and the remainder part is ignored. But, we also want the remainder thus we use bc utility. You can find more about bc utility in the man page of bc. For this do:
man bc

And, the scale=2 is assigned because we want decimal up to two places only. This is how our calculator looks like:

calculatorYou can also add power, modulus to the calculator. Copy the code and play with it. Enjoy calculating with your own made calculator!

0saves
Posted in Shell Scripting | Tagged , , , | 1 Comment

One Response to How to make your own calculator using shell script in Linux

  1. This one is quite informative and really helpful. I am searching a graphical menu that I can use using a mouse as well

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>