Learn Bash New

Basic Calculator Program | Learn Bash – Part 8

learn bash featured image 1

Welcome to the mini project of BASH scripting series. I hope you are excited! 🙂

We will build a simple calculator program using BASH scripting language 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. Then there were minor subtopics like using comments in bash programs and operator types.

Make sure to refer to those previous chapters in case you need to relearn lost information.

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 of them will have an exit option that will give the user the flexibility to exit the program if he/she wants 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 limitation.

For read command input, we will use while loop to implement an infinite control flow so the program exits only when it is indicated to by the user. The user will be asked for entering operand1 and operand2 values and the result will be computed based on his/her 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
With arguments output
Without arguments output

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 about an exit option so the user can terminate the program if he/she 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 ie., 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. 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 it with your pals, push yourself further by researching more on 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 languages out there.

Conclusion

BASH is a scripting language and the default shell interpreter for almost all major Linux distributions. So you have high scalability in case you wish to deploy cool Linux bash programs across multiple distributions. Moreover, now you can understand and also customize most software configuration files with the concepts learned here. They are all scripts 🙂

Our sponsor - Deploy your server within 3 minutes


Related articles

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More