Building simple calculator in BASH Building simple calculator in BASH shell shell

Building simple calculator in BASH


Quit a few errors here, I'll run through them and then show an example of a working script.

Firstly you appear to have made some assumptions about how functions work.

Calls to functions do not require the ()

addition()

Also you are trying to use global positional parameter in your functions which will not work as they have their own, so the call to the function should pass in what you want

addition $2 $3

With this in mind the inside of the function will also have to change

echo "The result of adding " + $1 + " and " + $2 + " is:"

As you can see we now use $1 and $2 as we are using the first and second parameter to the function,not the script!


Inside the function there are few more problems

d = expr $2 + $3

Spaces have a purpose in bash so will interfere with the = sign. The command is read as d(function/file/script/exe/whatever) and then the equals is a parameter to this. Thus you cannot have spaces between the = and both sides, so it should be written as

d=expr $2 + $3

Although this will still cause a compile error due to the spaces after expr.So we will need to run this in a subshell to assign it to d

d=$(expr $2 + $3)

Although personally i would just go for bash arithmetic

d=$(($2 + $3))

So if you change all of these in your script it should work, was gonna pad this out a bit more but ran out of time.


Working code

#!/bin/bashecho "Welcome to Calculator!"if [ $# != 3 ];then        echo "Usage:"        echo "Please enter a/s/m/d and two integers"        exit 1fiaddition () {        echo "The result of adding " + $1 + " and " + $2 + " is:"        d=$(($1 + $2))        echo $d}subtraction () {        echo "The result of subtracting " + $2 + " and " + $3 + " is:"        d=$(($1-$2))        echo $d}multiplication () {        echo "The result of multiply " + $1 + " and " + $2 + " is:"        d=$(($1*$2))        echo $d}division () {        echo "The result of dividing " + $1 + " and " + $2 + " is:"        d=$(($1/$2))        echo $d}if [[ "$1" = "a" ]]then        addition $2 $3        exit 0elif [[ "$1" = "s" ]]then        subtraction $2 $3        exit 0elif [[ "$1" = "m" ]]then        multiplication $2 $3        exit 0elif [[ "$1" = "d" ]]then        division $2 $3        exit 0else        echo "Usage:"        echo "Please enter a/s/m/d and two integers"        exit 1fi


To call a function in bash you don't need the parens, those are what's throwing you off. Instead just do

if [[ "$1" = "a" ]]then    addition    exit 0elif [[ "$1" = "s" ]]then    subtraction    exit 0elif [[ "$1" = "m" ]]then    multiplication    exit 0elif [[ "$1" = "d" ]]then    division    exit 0else    echo "Usage:"    echo "Please enter a/s/m/d and two integers"    exit 1fi

Also, for option "m" you had been calling subtraction again, I changed that to multiplication

That will get you past this error, I think you'll find more after that though


Simple Calculator 1.0

clearecho "(exp=**, div=/, mult=*, add=+, sub=-)"echo "\"a/b\" returns only quotient (use no spaces)"echo "\"a / b\" returns quotient and remainder (use spaces)"echo " "echo "Welcome to my Simple Calculator"echo " "read -p 'Enter a simple calculation: ' -a sCanswer=$((${sC[0]} ${sC[1]} ${sC[2]}))echo " "echo " "if [ "${sC[1]}" != "/" ]then echo "The answer is equal to $answer"else answerRemainder=$((${sC[0]} % ${sC[2]}))echo "The answer is equal to $answer remainder $answerRemainder"fiecho " "echo " "echo "Thank you for using Simple Calculator 1.0"echo "Have a nice day!"

No spaces between two expressions returns calculations without remainders:

(exp=**, div=/, mult=*, add=+, sub=-)"a/b" returns only quotient (use no spaces)"a / b" returns quotient and remainder (use spaces)Welcome to my Simple CalculatorEnter a simple calculation: 10/4The answer is equal to 2Thank you for using Simple Calculator 1.0Have a nice day!

Space between "/" will return a remainder:

(exp=**, div=/, mult=*, add=+, sub=-)"a/b" returns only quotient (use no spaces)"a / b" returns quotient and remainder (use spaces)Welcome to my Simple CalculatorEnter a simple calculation: 10 / 4The answer is equal to 2 remainder 2Thank you for using Simple Calculator 1.0Have a nice day!

You can make the first expression as long as you want with no spaces and multiply a second expression that contains addition, subtraction, multiplication, or division:

Enter a simple calculation: 1000/4/2/5 * 4+1    The answer is equal to 101Enter a simple calculation: 1000/4/2/5 *4-2The answer is equal to 98Enter a simple calculation: 1000/4/2/5 * 4*2The answer is equal to 200Enter a simple calculation: 1000/4/2/5 * 4/2The answer is equal to 50

However, Simple Calculator doesn't support remainder division of a second expression that contains any calculations not surrounded with brackets:If dividing a second expression with calculations the second expression must be surrounded with brackets in order to obtain the correct result:

Enter a simple calculation: 1000/4/2/5 / (4/2)The answer is equal to 12 remainder 1 

Brackets for the second expression are necessary!

1000/4/2/5 / 4/2 <---no brackets will return unpredictable results.Answer should be 12 remainder 1, but will return 3 remainder 0.

Enter a simple calculation: 1000/4/2/5 / 4/2The answer is equal to 3 remainder 0

Here in this case for remainder division, the brackets for the second expression are necessary.