TO CREATE A CALCULATOR

Shubham Soni
3 min readNov 21, 2023

--

First of all, create a new script file named newCal.sh.

sh means it is a script file and to write our script we are using the vim editor

$vim newCal.sh

and press i to work in insert mode in the editor.

Write the shebang line means we have to tell which shell we are currently using to run our script and provide a complete path.

#!/bin/bash

As we are creating a calculator we need some input, for that we are using “read -p” to take input from the user and storing the input in a variable called “fir”

read -p "Enter the first number- " fir

To perform any Arithmetic Operation we require another input, and for that, we require another input from the user. For that, we again use read -p and store the input in another variable called “sec”.

read -p "Enter the second number- " sec

Now we have to provide some options to the user so that the user can choose any option to perform any operation like ADD, SUB, MUL, or DIV.

To do this we are using the ‘echo’ command so that the options will be visible on the terminal and the user can select any of them like:

echo "Provide an Option to perform any task"
echo "a for addition"
echo "b for subtraction"
echo "c for multiplication"
echo "d for division"

Now in the next step, the user chooses one of the given options like a, b, c or d and writes on the terminal, we use a variable called “choice” and the given option is stored in the variable.

read choice

In the next step, we create some cases so that the operation is performed depending on the user’s requirement

For that, we will take the fir and sec variables as input and the choice of user to perform any operation. Here we use cases

case $choice in

If the user chooses the option ‘a’ then the script performs addition for the user and we can write it as

a) sum=$(($fir+$sec))
echo "Addition of $fir and $sec is $sum" ;;

the above code means we are creating a variable called ‘sum’ in which we are storing the values of the addition of fir and sec input, then we echo a line on the terminal as “Addition of input1 and input2 is ‘Result stored in sum’ ”.

then we add “;;” which means our option a is ended here.

Same with the Subtraction, Multiplication and Division

b) sub=$(($fir-$sec))
echo "Subtraction of $fir and $sec is $sub" ;;
c) mul=$(($fir*$sec))
echo "Multiplication of $fir and $sec is $mul" ;;
d) div=$(($fir/$sec))
echo "Division of $fir and $sec is $div" ;;
*) echo "Please enter a valid option" ;;

If the user does not provide any of the given options and enter any other key then, we use *

* Means all the keys other than a, b, c, and d will give an output as “Please enter a valid option” on the terminal.

In the last we have to close the case, for that, we use esac in our script.

esac

Here, the scripting is completed and to save the file and exit the editor press ESC and the write

:wq

As we have created the script above, now we have to execute or run the script.

There are the following ways to execute a script, which are as below:

1. use chmod +x newCal.sh to provide the execute permission of the file to all and then execute the script using ./newCal.sh

2. Direct execute using bash newCal.sh

OUTPUT-PICTURE

You can also check my GitHub-repo for the complete script:

Thank You….

--

--

No responses yet