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"
Toaster1
  • 13
  • 6
  • 1
    If you look up e.g. [How do I compare two string variables in an 'if' statement?](https://stackoverflow.com/questions/4277665/how-do-i-compare-two-string-variables-in-an-if-statement-in-bash) you'll see that none of them use quotes around `" if [[ $a == add ]] then"`. You shouldn't either. – that other guy May 06 '21 at 20:41
  • the compiler I used wouldn't run without it – Toaster1 May 06 '21 at 20:43
  • Syntax error: "elif" unexpected (expecting "then") is what shows up when I remove the "" – Toaster1 May 06 '21 at 20:45
  • What make & model is that? –  May 06 '21 at 20:47
  • Great. If you Google that error you get [BASH script expecting then, when I need else](https://stackoverflow.com/questions/14073294/bash-script-expecting-then-when-i-need-else), or if you run it through [ShellCheck](https://www.shellcheck.net) you get "SC1010: Use semicolon or linefeed before 'then'". – that other guy May 06 '21 at 20:49
  • Ubuntu, a linux operating system – Toaster1 May 06 '21 at 20:50
  • Remove the quotes around `if`s – KamilCuk May 06 '21 at 20:55
  • it returns unexpected ending without quotes around if – Toaster1 May 06 '21 at 21:02
  • @Toaster1 : May I suggest that you first fix the syntax problems, verify it on ShellCheck, and then ask a new question with a syntactically clean script, where you describe how you invoke your script, what result you get and what result you would expect. From your comments, you can't expect much helpful responses. – user1934428 May 07 '21 at 07:31
  • @Toaster1 : Also remove the _bash_ tag, since there is no bash involved in your code. – user1934428 May 07 '21 at 07:32

1 Answers1

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.
webb
  • 4,180
  • 1
  • 17
  • 26