Building a simple calculator in BASH

I am learning Bash Shell Scripting as a section from Linux Foundation LFS101x.2 and there is a lab for building a simple Bash calculator. Lab details can be found here: Lab 5

I am trying to run a script with:

$ ./bashShellScriptingLab5.sh s 15 5

      

Error message:

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

      

+3


source to share


2 answers


Complete a few mistakes here, I'll walk through them and then show an example of a working script.

First, you seem to have made some assumptions about how the functions work.

Function calls do not require ()

addition()

      

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

addition $2 $3

      

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

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

      

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


There are a few more problems inside the function



d = expr $2 + $3

      

Spaces have a purpose in bash, so they interfere with the = sign. The command is read as d (function / file / script / exe / whatever) and then equal is the parameter for that. This way you shouldn't have spaces between =

and on both sides, so it should be written as

d=expr $2 + $3

      

While this will still throw a compilation error due to whitespace after expr.So, we will need to run this in a subshell to assign it d

d=$(expr $2 + $3)

      

Though personally I would just go for bash arithmetic

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

      

So, if you change everything in your script, it should work, it would be a little more, but run 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

      

+2


source


You don't need partners to call a function in bash, this is what throws you off. Instead, just

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 called subtraction again, I changed that to multiply

This will allow you to get past this error, I think you will find more after that, though

+1


source







All Articles