Simple Bash Calculator Script


Terminal running my script

I tried to improve on the Calculator script, which we wrote last week in the tutorial, after the in-class assessment today. It was surprisingly short, but I won’t say how I thought it was as from experience I tend to jinx myself when I do so — a philosophy I have carried since year 3 when I ultimately failed to get into the OC class. That’s a somewhat redundant name.

In any case, I like my scripts to prompt people for input rather than numbers being inputted at the start of the script, but that’s just me as I often forget to input numbers and am greeted with a string of errors. My scripts tend to be polite and a little cheery.

The below is what I came up with, with some obnoxious text formatting here and there. set -e doesn’t seem to be working for some reason, but I haven’t had time to troubleshoot it properly.

#!/usr/bin/env bash

# Accept two numbers as arguments
# Allow user to select arithmetic sign and calculate result

set -e
bold=`tput bold`
normal=`tput sgr0`

echo -e "${bold}Please enter your first number:${normal} \c"
read ONE
echo -e "${bold}Please enter your second number:${normal} \c"
read TWO

echo "${bold}The numbers you chose were:${normal} $ONE and $TWO."
echo "Nice choices! What would you like to do next?"
echo "Press ${bold}1${normal} for +"
echo "Press ${bold}2${normal} for -"
echo "Press ${bold}3${normal} for *"
echo "Press ${bold}4${normal} for /"
echo "Sorry that we sound like a telephone service :("
echo -e "${bold}So what will it be?${normal} \c"
read SELECTION

if [ $SELECTION -eq 1 ]
  then
    OPERATION="addition"
    SIGN="+"
  elif [ $SELECTION -eq 2 ]
    then
      OPERATION="subtraction"
      SIGN="-"
    elif [ $SELECTION -eq 3 ]
      then
        OPERATION="multiplication"
        SIGN="*"
      else
          OPERATION="division"
          SIGN="/"
fi

echo "${bold}You selected $OPERATION!${normal}"
echo "Calculating.... Please wait."

CALCULATEDRESULT="$(( $ONE $SIGN $TWO ))"

echo "Let's see.... The answer you are looking for is: ${bold}$ONE $SIGN $TWO = $CALCULATEDRESULT${normal} :)"
echo "Have a nice day, please run me again!"

Perhaps I should have used $N1 and $N2 rather than $ONE and $TWO now I think about it.

I think I could improve the logic of the script somewhat, but it seems much improved from the previous one to me. Next on the list of improvements is to try and make it check if the user has actually entered a letter or a number and prompt for input again if it is a letter, as well as perhaps figuring out why it can’t calculate with decimals… though I’ve just also thought of a reason why that could be. Perhaps also make the inputting system more intuitive, as the current one does really resemble a phone system.

These kinds of things still elude me, but I hear I can do the former (checking for integer) with regex which we attempted to cover today, so it will be an interesting little project to see if I can get this to work and build this into something more functional.

Feels nice to have a little personal project to work on. :)