Garayed.com  

Go Back   Garayed.com > Linux
FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-14-2008, 08:28 AM
Matthew Lincoln
 
Posts: n/a
Default Execute second command only if first fails/succeeds?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 06-14-2008, 08:44 AM
Stephane CHAZELAS
 
Posts: n/a
Default Re: Execute second command only if first fails/succeeds?

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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



All times are GMT. The time now is 01:52 PM.




LinkBacks Enabled by vBSEO 3.0.0 © 2007, Crawlability, Inc.