in reply to Capture Exit Code Using Pipe & Tee

If your shell is bash, check out the PIPESTATUS shell parameter. For example, $command = "make -f <parameters> 2>&1 | tee -a build.log; let \\!PIPESTATUS"; might work, but I haven't tested it.

Replies are listed 'Best First'.
Re^2: Capture Exit Code Using Pipe & Tee
by Anonymous Monk on Jan 31, 2007 at 23:10 UTC
    merlyn's answer was dead on, for some reason it earlier it wasn't returning an error code, now with a minor tweak to merlyn's code I was able to get it working exactly like I wanted. THANKS A MILLION! This is a best place on the net for anything perl. I will be donating some $$$....

    my code for completeness:

    open MAKE, "make -f <params> 2>&1 |" or die; open (LOGFILE, ">>build.log") or die; while (<MAKE>) { print LOGFILE $_; print } close MAKE; # to get $? my $exit = $? >> 8; close LOGFILE;

    Edit: g0n - code tags

      the code wasn't actually returning the error code for the make file as far as i could tell. I used it to run any program, and had to add this at the end:
      exit $exit;

        the code wasn't actually returning the error code for the make file as far as i could tell.

        Correct, because that wasn't the question. The question was about obtaining make's exit code.

Re^2: Capture Exit Code Using Pipe & Tee
by meonkeys (Chaplain) on Mar 05, 2009 at 22:57 UTC
    PIPESTATUS does indeed work when using Bash. Nice trick!
    false | tee /dev/null [ $PIPESTATUS -eq 0 ] || exit $PIPESTATUS

    ---
    "A Jedi uses the Force for knowledge and defense, never for attack."