2

I have the following code in my bash script:

if [[ "$USE_NEW" == "true" ]]; then
    echo "Forcing usage of NEW build script"
    ./android/build/build.new.sh $@
elif [[ $MAJOR > 4 || ($MAJOR = 4 && $MINOR >= 2) ]]; then
    echo "Version greater than or equal to 4.2.x.x, running NEW build script"
    ./android/build/build.new.sh $@
else
    echo "Version less than 4.2.x.x, running LEGACY build script"
    #./android/build/build.legacy.sh $@
fi

I get an error:

line 149: expected `)'

Line 149 here happens to be the line containing the first elif in my example above. I don't know much about bash scripting and I can't find anything via Google that helps me find out what I'm doing wrong. Can anyone help correct my bash script?

3
  • 1
    Try putting spaces around the parens. Bash is very particular about separating tokens with whitespace in certain cases. Commented Apr 24, 2014 at 18:21
  • 1
    It's not easy to Google syntax errors. Try shellcheck instead (as described in the bash tag wiki), which automatically points out your problem. Commented Apr 24, 2014 at 18:45
  • @nneonneo: The problem here is not spaces. The problem is that >= is not a valid conditional operator, so it is actually parsed as though it were: $MINOR > = 4. Some operator is needed between the = and the 4 to make this a valid expression, so bash reports a syntax error. Commented Apr 24, 2014 at 18:54

1 Answer 1

4

Change your 2nd condition to:

[[ "$MAJOR" -gt 4 || ( "$MAJOR" -eq 4 && "$MINOR" -ge 2 ) ]]

EDIT: Or better:

(( MAJOR > 4 || ( MAJOR == 4 && MINOR >= 2) ))
Sign up to request clarification or add additional context in comments.

10 Comments

Or better yet, (( MAJOR > 4 || ( MAJOR == 4 && MINOR >= 2) )) - make use of arithmetic syntax when you're doing arithmetic... Of course, it might also then be a good idea to check that $MAJOR and $MINOR are actually numbers before-hand - but if they're not, even the -gt syntax is going to have issues...
Thanks @twalberg: I added that to my answer
Why is there a space after the first ( after the ||?
Even (( MAJOR > 4 || (MAJOR == 4 && MINOR >= 2) )) should also work
@RobertDailey The extra spaces inside the (( ... )) are cosmetic for readability and not required. Also, inside an arithmetic context, shell variables do not need to have the $ to dereference them (although including it is perfectly valid as well).
|

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.