bash: pass a return value down a pipe?
Hi. I am wondering: is it possible somehow to pass a return value down a pipeline in bash (or perhaps another shell scripting language?) A simple example would be something like so:
Code:
false | echo $? I was thinking that, if it were possible to pass return values down the pipe, one could use a wrapper program to create some kind of fail-pipe system, where the output of a pipe is either the output of the last command that failed, or the last command if none failed. |
I don't really see why you want to use a pipe here. A pipe is used to pass output of a program to another program. If the return code is all you need there is no need for a pipe at all, just break up your pipe at the points where you need to get the return code to work with it.
|
Quote:
PF parse "myfile" | PF refmtresults | PF updatedatabase | emailoutputto "admin@example.com" Anyway, that is the sort of thing I'm thinking of. Maybe a "failing" pipe isn't really necessary, being as there is a division between stderr and stdout (not sure). But I imagine there might be other possible applications as well. |
PIPE_STATUS is what you want to look at.
|
I have a similar question. Say I have a command like this:
Code:
make all 2>&1 | tee log.make.all |
Actually, I think it's PIPESTATUS (without the underscore).
example: Code:
$ true | false | true |
Wow, thank you!
|
Quote:
Code:
$ true | false | true |
Quote:
Quote:
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 You could possibly just echo the status code Code:
(foo && echo $?) | bar |
All times are GMT -5. The time now is 12:39 PM. |