Execute second command only if first fails/succeeds? - Networking

This is a discussion on Execute second command only if first fails/succeeds? - Networking ; 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 ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Execute second command only if first fails/succeeds?

  1. 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

  2. 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

  3. Re: Execute second command only if first fails/succeeds?

    kmlincoln100@hotmail.com (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


  4. Re: Execute second command only if first fails/succeeds?

    On Sat, 14 Jun 2008 14:22:20 +0000, Unruh wrote:

    > kmlincoln100@hotmail.com (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 to Thread