Networking Forums

Networking Forums > Computer Networking > Linux Networking > Execute second command only if first fails/succeeds?

Reply
Thread Tools Display Modes

Execute second command only if first fails/succeeds?

 
 
Matthew Lincoln
Guest
Posts: n/a

 
      06-14-2008, 08:28 AM
I am not sure about on how to specify that a second command should only be execute in case
that the first commnd fails/succeeds.

When I enter:

cmd1 | gzip -f ... && cmd2

Is then cmd2 executed only if cmd1 is finished successfully or if gzip is finished successfully?

What if I want cmd2 only to execute if both (cmd1 and gzip) end successfully?

What about the other way: cmd2 should only be executed if

a) cmd1 failed

b) cmd1 or gzip failed?

Matthew
 
Reply With Quote
 
 
 
 
Stephane CHAZELAS
Guest
Posts: n/a

 
      06-14-2008, 08:44 AM
2008-06-14, 08:28(+00), Matthew Lincoln:
> I am not sure about on how to specify that a second command should only be execute in case
> that the first commnd fails/succeeds.
>
> When I enter:
>
> cmd1 | gzip -f ... && cmd2
>
> Is then cmd2 executed only if cmd1 is finished successfully or if gzip is finished successfully?
>
> What if I want cmd2 only to execute if both (cmd1 and gzip) end successfully?
>
> What about the other way: cmd2 should only be executed if
>
> a) cmd1 failed
>
> b) cmd1 or gzip failed?

[...]

With the zsh shell:

setopt extendedglob

cmd1 | gzip
(( ${pipestatus[(I)^0]} )) || cmd2

Or:

cmd1 | gzip

(( $pipestatus[1] || $pipestatus[2] )) || cmd2

See question 11 in comp.unix.shell FAQ:

http://cfaj.freeshell.org/shell/cus-faq-2.html#11

for more details.

--
Stéphane
 
Reply With Quote
 
Unruh
Guest
Posts: n/a

 
      06-14-2008, 02:22 PM
(E-Mail Removed) (Matthew Lincoln) writes:

>I am not sure about on how to specify that a second command should only be execute in case
>that the first commnd fails/succeeds.


>When I enter:


>cmd1 | gzip -f ... && cmd2


>Is then cmd2 executed only if cmd1 is finished successfully or if gzip is finished successfully?


No. They go in order. the && will apply to gzip. BUt if cmd1 fails, the
pipe is broken and gzip fails anyway.

>What if I want cmd2 only to execute if both (cmd1 and gzip) end successfully?


>What about the other way: cmd2 should only be executed if


>a) cmd1 failed


>b) cmd1 or gzip failed?


man bash

 
Reply With Quote
 
Dan Stromberg
Guest
Posts: n/a

 
      06-30-2008, 01:10 AM
On Sat, 14 Jun 2008 14:22:20 +0000, Unruh wrote:

> (E-Mail Removed) (Matthew Lincoln) writes:
>
>>I am not sure about on how to specify that a second command should only
>>be execute in case that the first commnd fails/succeeds.

>
>>When I enter:

>
>>cmd1 | gzip -f ... && cmd2

>
>>Is then cmd2 executed only if cmd1 is finished successfully or if gzip
>>is finished successfully?

>
> No. They go in order. the && will apply to gzip. BUt if cmd1 fails, the
> pipe is broken and gzip fails anyway.
>
>>What if I want cmd2 only to execute if both (cmd1 and gzip) end
>>successfully?

>
>>What about the other way: cmd2 should only be executed if

>
>>a) cmd1 failed

>
>>b) cmd1 or gzip failed?

>
> man bash


The exit status of a pipeline in bash is the exit status of the last
command in the pipeline - but you can use set -o pipefail to make it
error for any command in the pipeline, not just the last.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cmd1 | gzip -f ... && cmd2 : Command concatenation with condition: Execute cmd2 only if cmd1 (or gzip) fails/succeeds? Matthew Lincoln Linux Networking 4 06-18-2008 12:58 PM
Nslookup succeeds, but ping fails to resolve hostname Curt McNamee Windows Networking 1 11-07-2006 03:04 PM
.net execute remotely Tom Windows Networking 0 06-30-2005 04:43 PM
Netdom verify fails in one direction, but succeeds in the other pehi Windows Networking 0 05-21-2005 07:21 AM
Bind succeeds for a used port prasad Linux Networking 2 04-16-2004 04:21 AM



1 2 3 4 5 6 7 8 9 10 11