Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
using && to test true with multiple conditions
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
brumik  
View profile  
 More options Feb 3, 10:44 am
Newsgroups: comp.unix.shell
From: brumik <brum...@gmail.com>
Date: Fri, 3 Feb 2012 07:44:44 -0800 (PST)
Local: Fri, Feb 3 2012 10:44 am
Subject: using && to test true with multiple conditions

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
pg@gmail.com  
View profile  
 More options Feb 6, 7:22 pm
Newsgroups: comp.unix.shell
From: "p...@gmail.com" <phil.ganc...@gmail.com>
Date: Mon, 6 Feb 2012 16:22:46 -0800 (PST)
Local: Mon, Feb 6 2012 7:22 pm
Subject: Re: using && to test true with multiple conditions
On Feb 3, 7:44 am, brumik <brum...@gmail.com> wrote:

command1 && ! command2 && action

See 'man bash', Section Compound Commands.

Or you can use:

if command1
then
    if ! command2
    then
        command
    fi
fi


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Gibson  
View profile  
 More options Feb 7, 10:23 am
Newsgroups: comp.unix.shell
From: dave.gma+news...@googlemail.com.invalid (Dave Gibson)
Date: Tue, 07 Feb 2012 15:23:18 +0000
Local: Tues, Feb 7 2012 10:23 am
Subject: Re: using && to test true with multiple conditions

brumik <brum...@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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stephane Chazelas  
View profile  
 More options Feb 7, 10:36 am
Newsgroups: comp.unix.shell
From: Stephane Chazelas <stephane_chaze...@yahoo.fr>
Date: Tue, 7 Feb 2012 15:36:57 +0000
Local: Tues, Feb 7 2012 10:36 am
Subject: Re: using && to test true with multiple conditions
2012-02-07 15:23:18 +0000, Dave Gibson:

[...]

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kaz Kylheku  
View profile  
 More options Feb 7, 12:37 pm
Newsgroups: comp.unix.shell
From: Kaz Kylheku <k...@kylheku.com>
Date: Tue, 7 Feb 2012 17:37:17 +0000 (UTC)
Local: Tues, Feb 7 2012 12:37 pm
Subject: Re: using && to test true with multiple conditions
On 2012-02-03, brumik <brum...@gmail.com> wrote:

> ie
><command1 is true>AND<command2 is false><execute action>

command1 && ! command2 && action

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Gibson  
View profile  
 More options Feb 7, 12:46 pm
Newsgroups: comp.unix.shell
From: dave.gma+news...@googlemail.com.invalid (Dave Gibson)
Date: Tue, 07 Feb 2012 17:46:16 +0000
Local: Tues, Feb 7 2012 12:46 pm
Subject: Re: using && to test true with multiple conditions

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

Flat out wrong.  Thank you for the correction.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »