Scripting with && and || in CommandBox 5.4.2

Just a note for anyone else who runs in to this, it turns out that CommandBox’s parsing of && and || in scripts are a little different than sh or bash.

Basically, each command is run left to right and is evaluated before looking at the rest of the script.

assertEqual 1 1 && echo "one"

will output one.

assertEqual 1 2 || echo "two"

will output two.

assertEqual 1 1 && echo "one" || echo "two"

will output one as well, but

assertEqual 1 2 && echo "one" || echo "two"

will exit with a non-zero exit code and output nothing.

So assertEqual and assertTrue can only evaluate either && or ||, not both.

If you need both, run a recipe. :slight_smile:

Yes, you can construct a basic conditional, but you can’t do an “else” sort of statement. Once an operator is reached that can’t be crossed (i.e. || after a passing exit code) the entire command chain is aborted there and goes no further.

If bash has a precedent otherwise, we can certainly look at doing that instead. To be honest, I didn’t look at bash here and without parenthesis to group operators, I didn’t think it made any logical sense to just jump ahead to another command in the chain as if nothing happened. It only made sense to me when I was writing this to stop processing. i.e. the || echo "two" portion of your last example is still all “downstream” of assertEqual 1 2 && and since the first command failed, I thought nothing after the && deserved to run.

But again, I don’t mind following bash if there’s a precedent, I just didn’t know about it,

Actually, I should have asked-- were you testing this on the latest version of CommandBox? I did make some changes to that logic the other day for Jon but I’ve forgotten exactly what it was,

OK, I just looked up the ticket
it actually looks like this is exactly what you are asking about and it’s been done since December-- I just totally forgot about it. Please test on the bleeding edge for me :slight_smile: