Basic Calculator Program - BASH Example Project [Part 8]
Table of Contents
We will build a bash example project, calculator program and at the same time reinforce all the concepts already taught. In a quick summary, we’ve explored the fundamental topics on variables, decisions, control statements, and arguments.
There were minor subtopics including using comments in bash programs and operator types.
Make sure to refer to previous articles in case you need to relearn any topic.
Algorithm or procedure
There are two ways to use this calculator program based on the input methods –
- Arguments – Use the program once
- read command – Use the program infinitely
Both procedures will have an exit option that will give the user the flexibility to exit the program if they want to.
For arguments input, we have to make sure that a maximum of 2 arguments is acceptable because, in mathematics, a simple addition, subtraction, division, or multiplication operation requires only two operands. Otherwise, we will print a message informing the user about the program’s limitations.
For read command input, we will use a while loop to implement an infinite control flow, so the program exits only when the user indicates it. The user will be asked to enter operand1 and operand2 values, and the result will be computed based on their choice.
Coding
#!/bin/bash
# Two arguments are passed as inputs.
# Will calculate based on the user choice
# And exit the calculator program straightaway
if [[ $# -eq 2 ]] ; then
# get operand1 and operand2 values from the arguments
operand1=$1
operand2=$2
# Offer choices
echo 1. Addition
echo 2. Subtraction
echo 3. Multiplication
echo 4. Division
echo 5. Exit
echo Enter your choice:
read choice
# start computing
if [[ $choice -eq 1 ]] ; then
echo ----------------------------------------
echo Addition of $operand1 and $operand2 is $((operand1+operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 2 ]] ; then
echo ----------------------------------------
echo Subtraction of $operand1 and $operand2 is $((operand1-operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 3 ]] ; then
echo ----------------------------------------
echo Multiplication of $operand1 and $operand2 is $((operand1*operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 4 ]] ; then
echo ----------------------------------------
echo Division of $operand1 and $operand2 is $((operand1/operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 5 ]] ; then
exit
else
echo ----------------------------------------
echo Invalid choice...
echo ----------------------------------------
echo
fi
# This portion will run the calculator program infinitely unless
# the exit option 5 is given as input by the user
elif [[ $# -eq 0 ]] ; then
while true
do
# Offer choices
echo 1. Addition
echo 2. Subtraction
echo 3. Multiplication
echo 4. Division
echo 5. Exit
echo Enter your choice:
read choice
# get operands and start computing based on the user's choice
if [[ $choice -eq 1 ]] ; then
echo Enter operand1 value:
read operand1
echo Enter operand2 value:
read operand2
echo ----------------------------------------
echo Addition of $operand1 and $operand2 is $((operand1+operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 2 ]] ; then
echo Enter operand1 value:
read operand1
echo Enter operand2 value:
read operand2
echo ----------------------------------------
echo Subtraction of $operand1 and $operand2 is $((operand1-operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 3 ]] ; then
echo Enter operand1 value:
read operand1
echo Enter operand2 value:
read operand2
echo ----------------------------------------
echo Multiplication of $operand1 and $operand2 is $((operand1*operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 4 ]] ; then
echo Enter operand1 value:
read operand1
echo Enter operand2 value:
read operand2
echo ----------------------------------------
echo Division of $operand1 and $operand2 is $((operand1/operand2))
echo ----------------------------------------
echo
elif [[ $choice -eq 5 ]] ; then
exit
else
echo ----------------------------------------
echo Invalid choice.. Please try again
echo ----------------------------------------
echo
fi
done
else
echo ----------------------------------------
echo You either passed too many parameters or too less
echo than the optimum requirement.
echo
echo This program accepts a maximum of 2 arguments or no
echo argument at all in order to run successfully.
echo ----------------------------------------
fi
Some clarification
In the above calculator program, you might notice two new things: infinite while loop and the keyword exit.
infinite while loop
There are many ways to implement an infinite loop using either for, while, or until loops. In our program, we’ve implemented it using a while loop.
while true
do
statements
done
Since the condition is always true, the loop executes infinitely. Of course, we did mention an exit option so the user can terminate the program if they wanted to. Right? We have the keyword…
exit
Once the bash interpreter encounters this keyword, it will directly terminate the program in execution. We’ve implemented an option, i.e., 5, to terminate the program, so the user is not stuck with the calculator program forever.
while true
do
...
elif [[ $choice -eq 5 ]] ; then
exit
done
Final thoughts
The code is quite lengthy, but lucky you. With this bash example project, you are getting a glimpse of the software project in real life. There’s much more to learn, but this series is intended to cover only the fundamentals, so you get the best benefits after learning this scripting language within a short span of time.
Now you can implement many cool programs and share them with your pals, push yourself further by researching BASH scripting, get comfortable involved in learning other major programming languages like C and C++, etc.
Of course, the syntax will vary and might get you puzzled at first, but you’ll notice that the concepts already taught here are the same for almost every programming language out there.
Conclusion
BASH is a scripting language and the default shell interpreter for almost all major Linux distributions. You have high scalability in case you wish to deploy Linux bash programs across multiple distributions. Moreover, now you can understand and customize most software configuration files with the concepts learned here. They are all scripts.
LinuxAndUbuntu Newsletter
Join the newsletter to receive the latest updates in your inbox.