Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: which command failed?



On Mar 28, 11:55pm, Atom Smasher wrote:
} Subject: which command failed?
}
} obviously the shell can already parse something like this:
} 
}  	foo || bar && baz | grep abc\|xyz
} 
} so, without reinventing the wheel, what's a good way to determine which of 
} those commands returned a non-zero exit status to the shell?

pipestatus <S> <Z>
     An array containing the exit statuses returned by all commands in
     the last pipeline.

You can't easily tell which of foo or bar may have returned a non-zero
status, but you can tell (in that particular example) when *both* of them
did, because $pipestatus will have only one element.  If $pipestatus has
two elements, then one of baz or grep returned nonzero.  Of course in
that case the entire pipeline has nonzero status if and only if grep was
the command that returned nonzero.

Beyond that, you'd have to rewrite the expression so that you can capture
$? in a shell variable before executing the next command.

  if if ! foo
     then
       foo_status=$?
       true
     fi ||
     if ! bar
     then
       bar_status=$?
       false
     else true
     fi
  then
     echo | false
     echo_status=$pipestatus[1]
     grep_status=$pipestatus[2]
  fi

That can be collapsed back down to and || && again by using { }.



Messages sorted by: Reverse Date, Date, Thread, Author