0

I am in the very early stages of learning Unix scripting. I've written a Bash script which does not generate any errors, but clearly has a logic error, as the IF test always gives the same response.

I have tried various variations on the theme of my IF, but still end up with the same result.

#!/bin/bash

declare -i number1
declare -i number2
declare -i total

declare operation

echo "Enter a, s, m or d for add, subtract, multiply or divide"
read operation

echo "Enter number 1"
read number1
echo "Enter number 2"
read number2

echo "operation="$operation

if [ $operation=='m' ]
then 
    total=$number1*$number2
elif  [ $operation=='a' ]
then 
    total=$number1+$number2
elif [ $operation=='d' ]
then 
    total=$number1/$number2
elif [ $operation=='s' ]
then 
    total=$number1-$number2
fi

echo $number1 " multiplied by " $number2 " equals " $total
exit 0

It doesn't matter whether I enter a, s or d (or indeed m) in response to the first prompt, my script always does a really nice multiplication... The line

echo "operation="$operation

correctly shows the operator I've requested.

Any ideas what I've done wrong?

Many thanks

5
  • 2
    I highly recommend pasting your script into shellcheck.net. Doing so will reveal that you need spaces around your == operators. (Plus a number of other recommended fixes.) Commented Sep 30, 2019 at 14:50
  • 1
    Even using == is a bad idea; = is the only POSIX-specified string comparison operator.= Commented Sep 30, 2019 at 15:01
  • 1
    Definite use shellcheck.net; you have other problems that will become apparent. Commented Sep 30, 2019 at 15:02
  • 1
    You might consider using a case statement here instead of a cascading if. Commented Sep 30, 2019 at 15:03
  • There are no 'operators' in this script. You are passing == as an argument to [. It is not an operator. Commented Sep 30, 2019 at 16:36

1 Answer 1

1

You need to add spaces around all the ==. That's it. Instead of

if [ $operation=='m' ]

you should have:

if [ $operation == 'm' ]
Sign up to request clarification or add additional context in comments.

3 Comments

Should be [ "$operation" = m ] to be compatible with all possible operation values and non-bash shells (= is the only POSIX-specified string comparison operator; expansions, but not literals, need quoting). That said, as this question is asked-and-answered already, the appropriate action is to vote to close this instance as a duplicate, not to add another answer. See stackoverflow.com/help/how-to-answer, particularly the "Answer Well-Asked Questions" section, particularly the bullet point regarding questions which "have already been asked and answered many times before".
...for a specific example of a case where [ $operation == 'm' ] will fail even on shells where == works, try with an operation of *; instead of just getting a false result you'll get a syntax error, because the * will be replaced with a list of filenames in the current directory before it's passed to the [ command.
Anyhow, much of the point of closing something as duplicate is that the canonical instance can collect answers that have already been vetted, refined and edited; whereas when you write a new answer on a new instance, all the work needed to make it pedantically correct and well-resourced is yet to be done.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.