Bash if command

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Bash if command

Stephen Grant Brown-2
In All,
 In info bash I see
--------------------------------------------------------------------------------------------------------------------------------
File: bash.info,  Node: Conditional Constructs,  Next: Command Grouping,
Prev:\
 Looping Constructs,  Up: Compound Commands

3.2.4.2 Conditional Constructs
..............................

`if'
     The syntax of the `if' command is:

          if TEST-COMMANDS; then
            CONSEQUENT-COMMANDS;
          [elif MORE-TEST-COMMANDS; then
            MORE-CONSEQUENTS;]
          [else ALTERNATE-CONSEQUENTS;]
          fi
--------------------------------------------------------------------------------------------

Where can a see a definition of "TEST-COMMANDS" ?

I wish to test the exit status of the command in a bash shell I have just
executed.

Stephen Brown


------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Bash if command

niXman
2013/2/12 Stephen Grant Brown:

> Where can a see a definition of "TEST-COMMANDS" ?
http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

> I wish to test the exit status of the command in a bash shell I have just
> executed.

$(any comand)
res=$?
if [[ $res == 0 ]] && { echo success } || { echo failure }


--
Regards,
niXman
___________________________________________________
Dual-target(32 & 64-bit) MinGW compilers for 32 and 64-bit Windows:
http://sourceforge.net/projects/mingwbuilds/
___________________________________________________
Another online IDE: http://liveworkspace.org/

------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Bash if command

Stephen Grant Brown-2
Hi niXman
----- Original Message -----
From: "niXman" <[hidden email]>
To: "MinGW Users List" <[hidden email]>
Sent: Tuesday, February 12, 2013 4:02 PM
Subject: Re: [Mingw-users] Bash if command


> 2013/2/12 Stephen Grant Brown:
>
>> Where can a see a definition of "TEST-COMMANDS" ?
> http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
>
>> I wish to test the exit status of the command in a bash shell I have just
>> executed.
>
> $(any comand)
> res=$?
> if [[ $res == 0 ]] && { echo success } || { echo failure }
>
>
> --
> Regards,
> niXman
Thanks

Stephen Brown

> ___________________________________________________
> Dual-target(32 & 64-bit) MinGW compilers for 32 and 64-bit Windows:
> http://sourceforge.net/projects/mingwbuilds/
> ___________________________________________________
> Another online IDE: http://liveworkspace.org/
>
> ------------------------------------------------------------------------------
> Free Next-Gen Firewall Hardware Offer
> Buy your Sophos next-gen firewall before the end March 2013
> and get the hardware for free! Learn more.
> http://p.sf.net/sfu/sophos-d2d-feb
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.  Disregard for the list
> etiquette may cause your account to be moderated.
>
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
> Also: mailto:[hidden email]?subject=unsubscribe


------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Bash if command

Keith Marshall
In reply to this post by niXman
On 12/02/13 05:02, niXman wrote:

> 2013/2/12 Stephen Grant Brown:
>> Where can a see a definition of "TEST-COMMANDS" ?
> http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
>
>> I wish to test the exit status of the command in a bash shell I have just
>> executed.
>
> $(any comand)
> res=$?
> if [[ $res == 0 ]] && { echo success } || { echo failure }

Ignore the above.  Not only is it hideously ugly; it relies on bash
specific syntax, (inherited from C-shell, IIRC), which it is *always*
best to avoid; (it doesn't answer the OP's question, and furthermore, it
is syntactically invalid).

   if any command
   then
     echo success
   else
     echo failure
   fi

is the syntax which *does* answer the question; an alternative, in this
case, (but not using "if", and therefore *not* directly answering the
question), could be:

   any command && echo success || echo failure

--
Regards,
Keith.

------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Bash if command

niXman
oops, sorry.

nixman-pc:~$ true;res=$?;[[ $res == 0 ]] && { echo success; } || {
echo failure; }
success
nixman-pc:~$ false;res=$?;[[ $res == 0 ]] && { echo success; } || {
echo failure; }
failure


--
Regards,
niXman
___________________________________________________
Dual-target(32 & 64-bit) MinGW compilers for 32 and 64-bit Windows:
http://sourceforge.net/projects/mingwbuilds/
___________________________________________________
Another online IDE: http://liveworkspace.org/

------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe
Reply | Threaded
Open this post in threaded view
|

Re: Bash if command

Keith Marshall
[Drifting off topic]

On 12 February 2013 08:44, niXman wrote:
> oops, sorry.
>
> nixman-pc:~$ true;res=$?;[[ $res == 0 ]] && { echo success; } || {
> echo failure; }
> success
> nixman-pc:~$ false;res=$?;[[ $res == 0 ]] && { echo success; } || {
> echo failure; }
> failure

But, why use such ugly, convoluted syntax in the first place?  If you
really want to promote yourself as a *nix guru, you should stick with
portable Bourne shell syntax:

  $ true && echo success || echo failure
  success

  $ false && echo success || echo failure
  failure

Not only is this significantly cleaner, and more elegant than your
contrived example, it is also portable to every UNIX system I have
worked on over the past 25 years; on many of them, yours will fail.

In this simple example, the braces are unnecessary, so don't use them.
(If you hadn't, you wouldn't have introduced the syntax error in your
original example).  They do become necessary, when you need to place
multiple commands in the true/false actions:

  $ true && {
      echo success; some other command; } || {
      echo failure; some recovery action; }

However, none of this is directly related to the syntax of an "if"
command block.

--
Regards,
Keith.

------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Also: mailto:[hidden email]?subject=unsubscribe