Table of Contents

In this article I will share a sample script which will act as an interactive calculator using shell or bash script. Using this interactive calculator you can perform basic and some advanced mathematical calculations like sine, cosine etc. We will use bc tool to create our interactive calculator.

## Sample Interactive Calculator - Shell Script

There will be two parts of our sample script wherein one part will be "`scriptbc`

" which we will add to our PATH variable so that it can be automatically executed instead of using absolute path

```
[root@node1 ~]# vim /usr/local/bin/scriptbc
#!/bin/bash
# scriptbc--Wrapper for 'bc' that returns the result of a calculation
if [ "$1" = "-p" ] ; then
precision=$2
shift 2
else
precision=2 # Default
fi
bc -q -l << EOF
scale=$precision
$*
quit
EOF
exit 0
```

Next we will copy our "scriptbc" tool to /usr/local/bin so that it becomes part of our system binary. We will use this tool to create our interactive calculator.

[root@node1 ~]# cp /tmp/scriptbc /usr/local/bin

Now the last part is to create our main wrapper script for creating our interactive calculator. You can modify the script to use as per your requirement.

```
[root@node1 ~]# vim /tmp/calculator.sh
#!/bin/bash
# calc--A command line calculator that acts as a frontend to bc
scale=2
show_help()
{
cat << EOF
In addition to standard math functions, calc also supports:
a % b remainder of a/b
a ^ b exponential: a raised to the b power
s(x) sine of x, x in radians
c(x) cosine of x, x in radians
a(x) arctangent of x, in radians
l(x) natural log of x
e(x) exponential log of raising e to the x
j(n,x) Bessel function of integer order n of x
scale N show N fractional digits (default = 2)
EOF
if [ $# -gt 0 ] ; then
exec scriptbc "$@"
fi
echo "Calc--a simple calculator. Enter 'help' for help, 'quit' to quit."
/bin/echo -n "calc> "
while read command args
do
case $command
in
quit|exit) exit 0 ;;
help|\?) show_help ;;
scale) scale=$args ;;
*) scriptbc -p $scale "$command" "$args" ;;
esac
/bin/echo -n "calc> "
done
echo ""
exit 0
```

## How it works?

The most interesting part of this code is the while read statement, which creates an infinite loop that displays the `calc>`

prompt until the user exits, either by entering quit or by entering an end-of-file sequence (`^D`

). The simplicity of this script is what makes it extra wonderful: shell scripts don’t need to be complex to be useful!

## Verify Interactive Calculator

By default, this script runs as an interactive tool that prompts the user for the desired actions. If invoked with arguments, those arguments are passed along to the `scriptbc`

command instead

[root@kerberos ~]# /tmp/calculator.sh Calc--a simple calculator. Enter 'help' for help, 'quit' to quit. calc> 4+5 9 calc> 4/5 .80 calc> 5/5 1.00 calc> 2/8 .25 calc> 7*7 49 calc> 2^2 4 calc> 2^7 128 calc> exit

`bc`

command can reveal some of these glitches in unexpected ways. For example, in `bc`

, set `scale=0`

and enter `7 % 3`

. Now try it with `scale=4`

. This produces `.0001`

, which is clearly incorrect.

Lastly I hope the steps from the article to create an interactive calculator using bash or shell script on Linux was helpful. So, let me know your suggestions and feedback using the comment section.

*References:*

*Wicked Cool Scripts*