real_command | filter_stuff | prettify | do_something_else
The problem arises when you try to figure out if your command succeeded or not. If you `echo $?` you'll get the return code for the last chain in the pipe. You don't really care about the output value of do_something_else, do you?
I haven't found a solution I really like to this problem, but this idiom is handy:
out=`real_command` && echo $out | filter_stuff | prettify | do_something_else
echo $?
Now $? will hold the value of real_command, and you can actually use it to diagnose the real problem.
Have you tried set -o pipefail? Here is the description.
ReplyDeleteIf set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.