Subscribe# mycmd_that_fails | postprocesor_that_succeeds
# echo $?
0
# mycmd_that_fails | postprocesor_that_succeeds
# echo $?
1
${PIPESTATUS[*]} in bash. I have something like this in my .bashrc, which gives me the exit status of every command in the last pipe I just ran, if anything is not 0:export PROMPT_COMMAND="
_RES=\${PIPESTATUS[*]};
_RES_STR='';
for res in \$_RES; do
if [[ ( \$res > 0 ) ]]; then
_RES_STR=\" [\$_RES]\";
fi;
done"
export PS1="\n\u@\h \w\$_RES_STR\n\\$ "Example usage:#!/bin/bash
PAT=$1
shift
"$@" 2>&1 | LANG=C egrep -ve "$PAT"
exit ${PIPESTATUS[0]}
filterout "Uninitialized" gcc dodgy.c
$ mkfifo pipe #make a pipe, named "pipe"
$ postprocesor_that_succeeds < pipe & #the call to read from pipe will block until a program writes to pipe
$ mycmd_that_fails > pipe
$ mkfifo pipe
$ cat < pipe &
$ bash #start a new shell, just for the exit value
$ exit 2 > pipe #exit out of new shell
$ echo $? #back in first shell
2
$You are not logged in, either login or create an account to post comments
mycmd | perl_thingy | postprocessor
Where the perl_thingy could stick $? in a file, then postprocessor could read that file. More or less complicated, depending on what you're actually doing and your personal tastes, would be to have second perl script that would accept the postprocessor's output, read the stashed result from the first, and generate whatever kind of summary you like.
posted by freebird at 3:10 PM on November 24, 2007