3

I have an if statement in my bash script as follows:

if [[ eb status my-env-staging-worker | grep 'Green' -ne 0 ] || [ eb status my-env-staging-web | grep 'Green' -ne 0 ]]

Basically if the first or second eb status command dont have the string Green I'd like to execute some other stuff.

However I get the following Error:

Conditional binary operator expected syntax error near status script returned exit code 2

Could you tell me whats wrong?

1 Answer 1

8

[[ and [ are not for grouping. They're independent commands. Instead of if [[ cmd1 -ne 0 ] || [ cmd2 -ne 0 ]], leave out the brackets and the tests and simply write if cmd1 || cmd2.

if eb status my-env-staging-worker | grep -q 'Green' || eb status my-env-staging-web | grep -q 'Green'

I've added -q to suppress grep's output since you only care about the return code.

If you want to invert the condition, write:

if ! eb status my-env-staging-worker | grep -q 'Green' && ! eb status my-env-staging-web | grep -q 'Green'

or

if ! { eb status my-env-staging-worker | grep -q 'Green' || eb status my-env-staging-web | grep -q 'Green'; }

Here you can see { and } used for grouping. Curly braces and parentheses are bash's grouping tokens.

Sign up to request clarification or add additional context in comments.

3 Comments

Seems like the OP wants the opposite, so some ! are needed.
I guess the opposite was ambiguous, but I think it should be ! cmd1 || ! cmd2.
Yeah, it's ambiguous. OP can make the proper adjustment if I interpreted the question wrong.

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.