Skip to main content
deleted 1 character in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Two options:

  1. set the pipefail option from ksh (set -o pipefail), then the exit status of a pipeline will be that of the right-most pipeline component that failed.

    set -o pipefail
    if ! A | B | C; then
      echo at least one of the commands failed.
    fi
    
  2. look for non-zero values in $pipestatus. With the extendedglob option enabled, $pipestatus[(r)^0] (or $pipestatus[(r)<1->] which doesn't require extendedglob) will return the exit status of the left-most failing pipeline component and $pipestatus[(R)^0] of the right-most, so you can do:

    set -o extendedglob
    A | B | C
    if (( $pipestatus[(r)^0] )); then
      echo at least one of the commands failed.
    fi
    

    or

    A | B | C
    if (( $pipestatus[(r)<1->] )); then
      echo at least one of the commands failed.
    fi
    

    With the I subscript flag instead of r/R, you'll get the Index of the right-most matching element, so you can do:

    A | B | C
    if (( failed = $pipestatus[(I)<1->] )); then
      echo component $failed failed.
    fi
    

    ${pipestatus:#0} will expand to the components of $pipestatus except 0, so you could also do:

    A | B | C
    failed_statuses=( ${pipestatus:#0} )
    if (( $#failed_statuses )); then
      echo "${#failed_statuses} command(s) failed. Statuses: $failed_statuses"
    fi
    

Two options:

  1. set the pipefail option from ksh (set -o pipefail), then the exit status of a pipeline will be that of the right-most pipeline component that failed.

    set -o pipefail
    if ! A | B | C; then
      echo at least one of the commands failed.
    fi
    
  2. look for non-zero values in $pipestatus. With the extendedglob option enabled, $pipestatus[(r)^0] (or $pipestatus[(r)<1->] which doesn't require extendedglob) will return the exit status of the left-most failing pipeline component and $pipestatus[(R)^0] of the right-most, so you can do:

    set -o extendedglob
    A | B | C
    if (( $pipestatus[(r)^0] )); then
      echo at least one of the commands failed.
    fi
    

    or

    A | B | C
    if (( $pipestatus[(r)<1->] )); then
      echo at least one of the commands failed.
    fi
    

    With the I subscript flag instead of r/R, you'll get the Index of the right-most matching element, so you can do:

    A | B | C
    if (( failed = $pipestatus[(I)<1->] )); then
      echo component $failed failed.
    fi
    

Two options:

  1. set the pipefail option from ksh (set -o pipefail), then the exit status of a pipeline will be that of the right-most pipeline component that failed.

    set -o pipefail
    if ! A | B | C; then
      echo at least one of the commands failed.
    fi
    
  2. look for non-zero values in $pipestatus. With the extendedglob option enabled, $pipestatus[(r)^0] (or $pipestatus[(r)<1->] which doesn't require extendedglob) will return the exit status of the left-most failing pipeline component and $pipestatus[(R)^0] of the right-most, so you can do:

    set -o extendedglob
    A | B | C
    if (( $pipestatus[(r)^0] )); then
      echo at least one of the commands failed.
    fi
    

    or

    A | B | C
    if (( $pipestatus[(r)<1->] )); then
      echo at least one of the commands failed.
    fi
    

    With the I subscript flag instead of r/R, you'll get the Index of the right-most matching element, so you can do:

    A | B | C
    if (( failed = $pipestatus[(I)<1->] )); then
      echo component $failed failed.
    fi
    

    ${pipestatus:#0} will expand to the components of $pipestatus except 0, so you could also do:

    A | B | C
    failed_statuses=( ${pipestatus:#0} )
    if (( $#failed_statuses )); then
      echo "${#failed_statuses} command(s) failed. Statuses: $failed_statuses"
    fi
    
deleted 1 character in body
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Two options:

  1. set the pipefail option from ksh (set -o pipefail), then the exit status of a pipeline will be that of the leftright-most pipeline component that failed.

    set -o pipefail
    if ! A | B | C; then
      echo at least one of the commands failed.
    fi
    
  2. look for non-zero values in $pipestatus. With the extendedglob option enabled, $pipestatus[(r)^0] (or $pipestatus[(r)<1->] which doesn't require extendedglob) will return the exit status of the left-most failing pipeline component and $pipestatus[(R)^0] of the right-most, so you can do:

    set -o extendedglob
    A | B | C
    if (( $pipestatus[(r)^0] )); then
      echo at least one of the commands failed.
    fi
    

    or

    A | B | C
    if (( $pipestatus[(r)<1->] )); then
      echo at least one of the commands failed.
    fi
    

    With the I subscript flag instead of r/R, you'll get the Index of the right-most matching element, so you can do:

    A | B | C
    if (( failed = $pipestatus[(I)<1->] )); then
      echo component $failed failed.
    fi
    

Two options:

  1. set the pipefail option from ksh (set -o pipefail), then the exit status of a pipeline will be that of the left-most pipeline component that failed.

    set -o pipefail
    if ! A | B | C; then
      echo at least one of the commands failed.
    fi
    
  2. look for non-zero values in $pipestatus. With the extendedglob option enabled, $pipestatus[(r)^0] (or $pipestatus[(r)<1->] which doesn't require extendedglob) will return the exit status of the left-most failing pipeline component and $pipestatus[(R)^0] of the right-most, so you can do:

    set -o extendedglob
    A | B | C
    if (( $pipestatus[(r)^0] )); then
      echo at least one of the commands failed.
    fi
    

    or

    A | B | C
    if (( $pipestatus[(r)<1->] )); then
      echo at least one of the commands failed.
    fi
    

Two options:

  1. set the pipefail option from ksh (set -o pipefail), then the exit status of a pipeline will be that of the right-most pipeline component that failed.

    set -o pipefail
    if ! A | B | C; then
      echo at least one of the commands failed.
    fi
    
  2. look for non-zero values in $pipestatus. With the extendedglob option enabled, $pipestatus[(r)^0] (or $pipestatus[(r)<1->] which doesn't require extendedglob) will return the exit status of the left-most failing pipeline component and $pipestatus[(R)^0] of the right-most, so you can do:

    set -o extendedglob
    A | B | C
    if (( $pipestatus[(r)^0] )); then
      echo at least one of the commands failed.
    fi
    

    or

    A | B | C
    if (( $pipestatus[(r)<1->] )); then
      echo at least one of the commands failed.
    fi
    

    With the I subscript flag instead of r/R, you'll get the Index of the right-most matching element, so you can do:

    A | B | C
    if (( failed = $pipestatus[(I)<1->] )); then
      echo component $failed failed.
    fi
    
Source Link
Stéphane Chazelas
  • 584.8k
  • 96
  • 1.1k
  • 1.7k

Two options:

  1. set the pipefail option from ksh (set -o pipefail), then the exit status of a pipeline will be that of the left-most pipeline component that failed.

    set -o pipefail
    if ! A | B | C; then
      echo at least one of the commands failed.
    fi
    
  2. look for non-zero values in $pipestatus. With the extendedglob option enabled, $pipestatus[(r)^0] (or $pipestatus[(r)<1->] which doesn't require extendedglob) will return the exit status of the left-most failing pipeline component and $pipestatus[(R)^0] of the right-most, so you can do:

    set -o extendedglob
    A | B | C
    if (( $pipestatus[(r)^0] )); then
      echo at least one of the commands failed.
    fi
    

    or

    A | B | C
    if (( $pipestatus[(r)<1->] )); then
      echo at least one of the commands failed.
    fi