0

I get this error:

getengine.sh: line 38: [: =: unary operator expected
getengine.sh: line 41: [: =: unary operator expected

Here is the script:

if [ $1 = "m" ]; then #this is line 38
    initial=`curl -s http://some/2016/30-publish`
    label=`curl -s http://test/${initial}.l/artifacts/info.xml`
elif [ $1 = "s" ]; then # this is line 41
    initial=`curl -s http://some/2016/30-publish`
    label=`curl -s http://test/${initial}.l/artifacts/info.xml`
fi

I don't understand what is wrong? This works perfectly locally but when I run it in a linux environment server I get this error...any help would be appreciated

3
  • shellcheck.net detects this issue automatically. Commented Oct 25, 2016 at 15:24
  • ...btw, what do you expect your code to do when neither m nor s is passed? Commented Oct 25, 2016 at 15:26
  • I have an else to print out usage, btw the link you posted has no accepted answer: stackoverflow.com/questions/22179405/… Commented Oct 25, 2016 at 15:42

1 Answer 1

3

You need more quotes.

# this is correct
[ "$1" = "m" ]

...not...

# WRONG: this becomes [ = m ] if $1 is empty, producing your error.
[ $1 = "m" ]
Sign up to request clarification or add additional context in comments.

7 Comments

You sure, why is this working locally for me?
I'm running locally with a mac
@arabian_albert, yes, I'm sure. The reason it's working locally for you is that you aren't testing with $1 being empty, whereas it's empty when it runs on the server.
@arabian_albert, ...though you could also be masking the problem by using a non-POSIX-compliant shell such as zsh for your local testing, which doesn't follow POSIX-mandated rules regarding string-splitting of unquoted expansions.
Just because it's hardcoded doesn't mean it can't be emptied later. A bad set command could do that, f'rinstance, or a shift. Use bash -x yourscript to see what's actually happening in practice during execution.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.