In bash if we use && to execute the right hand side, if the left side
is true:
e.g.
echo "true" && true
Can I combine this with another command which is not true?
In simple terms i want to run 2 commands, and only execute the action,
if command1 returns true and command2 returns false, on one line
without any if statements, how do I do that?
ie
<command1 is true>AND<command2 is false><execute action>
> In bash if we use && to execute the right hand side, if the left side
> is true:
> e.g.
> echo "true" && true
> Can I combine this with another command which is not true?
> In simple terms i want to run 2 commands, and only execute the action,
> if command1 returns true and command2 returns false, on one line
> without any if statements, how do I do that?
> ie
> <command1 is true>AND<command2 is false><execute action>
> In bash if we use && to execute the right hand side, if the left side
> is true:
> e.g.
> echo "true" && true
> Can I combine this with another command which is not true?
> In simple terms i want to run 2 commands, and only execute the action,
> if command1 returns true and command2 returns false, on one line
> without any if statements, how do I do that?
With the list "or" (failure / false) operator '||'.
command1 && command2 || command3
The && and || list operators have equal precedence giving simple left to
right evaluation with no implicit grouping. Each operator applies only
to the preceding command. Execution of the list ends when a command's
exit status does not match the operator to the right of that command.
It may be clearer to insert a newline after each list operator (at least
temporarily) so that "a && b || c" becomes:
a && # - continue only if "a" succeeds
b || # - continue only if "b" fails
c
> > In bash if we use && to execute the right hand side, if the left side
> > is true:
> > e.g.
> > echo "true" && true
> > Can I combine this with another command which is not true?
> > In simple terms i want to run 2 commands, and only execute the action,
> > if command1 returns true and command2 returns false, on one line
> > without any if statements, how do I do that?
> With the list "or" (failure / false) operator '||'.
> command1 && command2 || command3
> The && and || list operators have equal precedence giving simple left to
> right evaluation with no implicit grouping. Each operator applies only
> to the preceding command. Execution of the list ends when a command's
> exit status does not match the operator to the right of that command.
[...]
That statement is confusing at best, and probably wrong since
the code above doesn't answer the questions
$ (exit 12) && (exit 23) || echo $?
12
command3 was run even though command1 failed.
Generally, combining && with || at the same time is a bad idea.
The common mistake being
cmd && then-part || else-part
Yes, else-part will be run if cmd fails, but it will also be run
if "then-part" fails.
Stephane Chazelas <stephane_chaze...@yahoo.fr> wrote:
> 2012-02-07 15:23:18 +0000, Dave Gibson:
>> The && and || list operators have equal precedence giving simple left to
>> right evaluation with no implicit grouping. Each operator applies only
>> to the preceding command. Execution of the list ends when a command's
>> exit status does not match the operator to the right of that command.
> [...]
> That statement is confusing at best, and probably wrong since
> the code above doesn't answer the questions