Let's say I do this in a unix shell
$ some-script.sh | grep mytext
$ echo $?
this will give me the exit code of grep
but how can I get the exit code of some-script.sh
EDIT
Assume that the pipe operation is immutable. ie, I can not break it apart and run the two commands seperately
If you're using bash:
There is a utility named
mispipe
which is part of the moreutils package.It does exactly that:
mispipe some-script.sh 'grep mytext'
There are multiple solutions, it depends on what you want to do exactly.
The easiest and understandable way would be to send the output to a file, then grep for it after saving the exit code:
A trick from the comp.unix.shell FAQ (#13) explains how using the pipeline in the Bourne shell should help accomplish what you want:
First approach, temporarly save exit status in some file. This cause you must create subshell using braces:
another approach presented by Randy above, simplier code implementation:
its all. both works under bash (i know, bashizm). good luck :) both approaches does not save temporarly pipe to physical file, only exit code.