1

I'm trying to write a script that will accept positional arguments (command-line arguments) that specify a mathematical operation (add, sub, mult, div, mod) and two values. The script is supposed to get this input, compute the result, and display the answer. I'm not sure where I went wrong because when I try to run my script, this is what I get

./trial.sh mod 15 10
./trial.sh: 6:        if [[ mod == add ]] then: not found
Result:  25
./trial.sh: 9:        elif [[ mod == sub ]] then: not found
Result:  5
./trial.sh: 12:        elif [[ mod == mult ]] then: not found
Result:  150
./trial.sh: 15:        elif [[ mod == div ]] then: not found
Result:  1
./trial.sh: 18:        elif [[ mod == mod ]] then: not found
Result:  5
./trial.sh: 21:        else: not found
error
./trial.sh: 23: fi: not found

This is the code I used that presented that result

#!/bin/bash
        a=$1
        b=$2
        c=$3
        result=0        
"       if [[ $a == add ]] then"
           result=$((b + c))
           echo "Result: " $result
"       elif [[ $a == sub ]] then"
           result=$((b - c))
           echo "Result: " $result
"       elif [[ $a == mult ]] then"
           result=$((b * c))
           echo "Result: " $result
"       elif [[ $a == div ]] then"
           result=$((b / c))
           echo "Result: " $result
"       elif [[ $a == mod ]] then"
           result=$((b % c))
           echo "Result: " $result
"       else"
           echo "error"
        "fi"
10
  • 1
    If you look up e.g. How do I compare two string variables in an 'if' statement? you'll see that none of them use quotes around " if [[ $a == add ]] then". You shouldn't either. Commented May 6, 2021 at 20:41
  • the compiler I used wouldn't run without it Commented May 6, 2021 at 20:43
  • Syntax error: "elif" unexpected (expecting "then") is what shows up when I remove the "" Commented May 6, 2021 at 20:45
  • What make & model is that? Commented May 6, 2021 at 20:47
  • Great. If you Google that error you get BASH script expecting then, when I need else, or if you run it through ShellCheck you get "SC1010: Use semicolon or linefeed before 'then'". Commented May 6, 2021 at 20:49

1 Answer 1

4

Your sample code is incorrect in several ways. You wrapped some of your statements in double-quotes to make the errors stop, but this is a bit like taking the engine out of a car because it's making a weird noise.

Here are three things you can do immediately to make your code work:

  1. take away the double-quotes so that your code is code statements again and not strings. If you want to temporarily disable some shell code, put # before it to comment it out, e.g.,
# don't wrap broken code in double-quotes:
"if[x= 1]] then rm -rf /"
# instead, comment it out, like this:
# if[x= 1]] then rm -rf /
  1. in bash if...then, a newline or semicolon is required before then , e.g.,
if [[ $a == 1 ]] then   # wrong

if [[ $a == 1 ]]; then  # ok

if [[ $a == 1 ]]        # also ok
then
  1. change #!/bin/sh to #!/bin/bash because many variants of sh don't support double-bracket [[ tests.
Sign up to request clarification or add additional context in comments.

1 Comment

This helped. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.