Quote:
Originally Posted by hydraMax
I think that PIPESTATUS does not work for what I wish to accomplish, because it suffers from the same issue as $?; i.e., it gives status information on the /last pipeline completed/, not the current pipeline.
|
No, it won't work for you. The piped commands are executed in
subshells, so they can not be aware of each other's exit codes.
Quote:
Originally Posted by hydraMax
But if one of the commands fails, then the output of the whole pipe is the output of the last command that failed.
|
"Last command that failed"? What's that? The processes are
not run one after another. They all run simultaneously. The fact that their
stdin and
stdout file descriptors are redirected in a certain way does not make one process depend on the others. The commands in a pipe are sent to subshells regardless of what they do. It's up to each of these subshells to handle executing of the command they were given. None of these subshells knows anything about any of the others. It just gets input from somewhere and writes output somewhere.
The subshell then exits and sends its exit status to the parent shell. Only after
all of the subshells exit, the
$? and
$PIPESTATUS are set.
If you have something like
Code:
grep foo file | grep bar | grep baz
By the time
grep bar returns non-zero exit status, it is already too late to decide you want to print its output on screen instead of the output of
grep baz.
You could possibly just echo the status code
Code:
(foo && echo $?) | bar
but I don't think it would be very useful. If you think you need to know the exit status of a piped command on the other side of the pipe, your program is probably poorly designed.