Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

using && to test true with multiple conditions

16 views
Skip to first unread message

brumik

unread,
Feb 3, 2012, 10:44:44 AM2/3/12
to

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>

thankyou for any help.





phil.g...@gmail.com

unread,
Feb 6, 2012, 7:22:46 PM2/6/12
to
command1 && ! command2 && action

See 'man bash', Section Compound Commands.

Or you can use:

if command1
then
if ! command2
then
command
fi
fi

Dave Gibson

unread,
Feb 7, 2012, 10:23:18 AM2/7/12
to
brumik <bru...@gmail.com> wrote:
>
> 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

Stephane Chazelas

unread,
Feb 7, 2012, 10:36:57 AM2/7/12
to
2012-02-07 15:23:18 +0000, Dave Gibson:
> brumik <bru...@gmail.com> wrote:
> >
> > 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

Kaz Kylheku

unread,
Feb 7, 2012, 12:37:17 PM2/7/12
to
On 2012-02-03, brumik <bru...@gmail.com> wrote:
> ie
><command1 is true>AND<command2 is false><execute action>

Dave Gibson

unread,
Feb 7, 2012, 12:46:16 PM2/7/12
to
Stephane Chazelas <stephane...@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

Flat out wrong. Thank you for the correction.
0 new messages