# mycmd_that_fails | postprocesor_that_succeeds # echo $? 0And, what I'd like to see is:
# mycmd_that_fails | postprocesor_that_succeeds # echo $? 1The obvious solution was to capture the output of mycmd_that_fails into a file and then pipe it through the postprocesor_that_succeeds, but since my command is a build script it's nice to see the progress as it runs. I'd rather have the postprocessor operate on the command as it runs than after the fact.
${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\\$ "#!/bin/bash
PAT=$1
shift
"$@" 2>&1 | LANG=C egrep -ve "$PAT"
exit ${PIPESTATUS[0]}
Example usage:posted by jepler at 3:46 PM on November 24, 2007filterout "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
$
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