retval of pipelined cmd in bash

Randall R Schulz
Fri Dec 20 14:54:00 GMT 2002


If you wrote all that yourself, I can't say you look like a BASH newbie. 
Then again, there's that "too clever by half" thing...

As an aside, I find "export NAME=value" to be clearer than "declare -x 
NAME=value", but that's just opinion.

As to your problem, first of all, you're only capturing the exit code when 
the make fails. Replace the "||" with a semicolon and ECODE will never be 

Secondly, you're using an odd way to test $ECODE. I usually use something 
like this:

if [ "$ECODE" -eq 0 ]; then
     echo Safe and sound.

     echo Uh-Oh!

This has the added advantage of being portable to simpler shells like 
Bourne and Ash. I'm not sure about the expression facilities represented by 
use of the double-square-bracket notation.

Randall Schulz

P.S. No one's going to come after you if you utter the word Unix.

Unix Unix Unix Unix Unix Unix Unix Unix Unix Unix Unix Unix Unix Unix Unix


Hey! Wait!! Stop that!!! You can't take my computer.
HEY! I paid for that software.
HEY!!! I WROTE that software!!

At 13:47 2002-12-20, Soren A wrote:
>Hallo Cygwains,
>[Heck, I dunno... "Cygwinauts"?].
>I have a possibly OT question, that is, a bash shell question. Lacking
>the insight into the deepest reaches of shell-ology, I have come up
>empty on all attempts to solve this one for myself. (Those attempts have
>included doing `info bash' and reading the "Advanced Bash Scripting
>Guide", an excellent resource available on the Web).
>I am writing a shell script that chains together several commands in a
>pipeline -- very *nix-ish. The first command in the pipeline is an
>invocation of `make'. Here's the entire script code -- I've tried many
>very elaborate or bizarre things, before this:
>    function powermake
>    {
>     declare -x LESS='-z-2$ -s~wR'
>     declare PAGER='/bin/less'
>     declare +x ECODE=
>     { make "$@" || ECODE=$? ; } 2>&1 | tee $MAKE_ERR_TO | \
>         /cdv/f/scr/ | $PAGER -O"${MAKE_ERR_TO}.colorlog"
>     if [[ $ECODE ]]  # "0" and "1" are both TRUE; "null" is FALSE.
>        then $gvimexe --servername 'QUICKFIX' -q "$(cygpath -wla 
>        else echo 'No "make" errors to diagnose: retval was ' '"'$ECODE'".'
>     fi
>     return $ECODE
>    }
>The problem I am trying to solve is how to get the return value of the
>`make' tool. If there was an error I need to know about it. But as it
>stands, nothing that i have tried will cause me to see the VIM editor
>session start up; the value in ECODE is apparently always "0".
>Does anyone know how to do this -- how to pull out a return value from a
>command in the middle of a pipeline?
>    Thanks,
>      Soren

Unsubscribe info:
Bug reporting:

More information about the Cygwin mailing list