Two options:
set the
pipefailoption 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. filook for non-zero values in
$pipestatus. With theextendedgloboption enabled,$pipestatus[(r)^0](or$pipestatus[(r)<1->]which doesn't requireextendedglob) 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. fior
A | B | C if (( $pipestatus[(r)<1->] )); then echo at least one of the commands failed. fiWith the
Isubscript flag instead ofr/R, you'll get theIndex 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$pipestatusexcept 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