Building simple calculator in BASH

I am learning Bash Shell Scripting as a section from the Linux Foundation LFS101x.2 and there is a Lab to create a simple Bash calculator. The Lab details are found here: Lab 5

I'm trying to run the script by:

$ ./bashShellScriptingLab5.sh s 15 5

The error message is:

Welcome to Calculator!
./bashShellScriptingLab5.sh: line 39: syntax error near unexpected token `exit'
./bashShellScriptingLab5.sh: line 39: ` exit 0'

Here is my bashShellScriptingLab5.sh:

#!/bin/bash

echo "Welcome to Calculator!"

if [ $# != 3 ];
then
        echo "Usage:"
        echo "Please enter a/s/m/d and two integers"
        exit 1
fi

addition () {
        echo "The result of adding " + $2 + " and " + $3 + " is:"
        d = expr $2 + $3
        echo $d
}

subtraction () {
        echo "The result of subtracting " + $2 + " and " + $3 + " is:"
        d = expr $2 - $3
        echo $d
}

multiplication () {
        echo "The result of multiply " + $2 + " and " + $3 + " is:"
        d = expr $2 * $3
        echo $d
}

division () {
        echo "The result of dividing " + $2 + " and " + $3 + " is:"
        d = expr $2 / $3
        echo $d
}

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

Upvotes: 4

Views: 8952

Answers (3)

mOmOney
mOmOney

Reputation: 373

Simple Calculator 1.0

clear
echo "(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 sC
answer=$((${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"
fi
echo " "
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 Calculator

Enter a simple calculation: 10/4


The answer is equal to 2


Thank you for using Simple Calculator 1.0
Have 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 Calculator

Enter a simple calculation: 10 / 4


The answer is equal to 2 remainder 2


Thank you for using Simple Calculator 1.0
Have 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 101


Enter a simple calculation: 1000/4/2/5 *4-2
The answer is equal to 98

Enter a simple calculation: 1000/4/2/5 * 4*2
The answer is equal to 200

Enter a simple calculation: 1000/4/2/5 * 4/2
The 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/2


The answer is equal to 3 remainder 0

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

Upvotes: 0

123
123

Reputation: 11216

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/bash

echo "Welcome to Calculator!"

if [ $# != 3 ];
then
        echo "Usage:"
        echo "Please enter a/s/m/d and two integers"
        exit 1
fi

addition () {
        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 0
elif [[ "$1" = "s" ]]
then
        subtraction $2 $3
        exit 0
elif [[ "$1" = "m" ]]
then
        multiplication $2 $3
        exit 0
elif [[ "$1" = "d" ]]
then
        division $2 $3
        exit 0
else
        echo "Usage:"
        echo "Please enter a/s/m/d and two integers"
        exit 1
fi

Upvotes: 3

Eric Renouf
Eric Renouf

Reputation: 14500

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 0
elif [[ "$1" = "s" ]]
then
    subtraction
    exit 0
elif [[ "$1" = "m" ]]
then
    multiplication
    exit 0
elif [[ "$1" = "d" ]]
then
    division
    exit 0
else
    echo "Usage:"
    echo "Please enter a/s/m/d and two integers"
    exit 1
fi

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

Upvotes: 1

Related Questions