command does not return exit status due to tee

Tags
command, exit status, shell script, shell scripting, shell scripts, tee, unix scripting, unix scripting basics

 
Thread Tools Search this Thread
# 1  
Old 12-05-2008
Tools command does not return exit status due to tee

Hi,

I am using /bin/sh. I want to display the stdout and stderr on the terminal as well as save it in a file, so I'm using this command.

gmake all 2>&1 | tee log

But even if gmake fails, it's always giving 0 as exit status, i suppose because of tee.

# false 2>&1 | tee Log
# echo $?
0

I was expecting non-zero value above. Any solution to this problem?

What if I want to use tee as well as get correct exit status?
# 2  
Old 12-05-2008
The exit status in your case will always be that of tee and tee seems successful, so you get a zero. You would have to get it before you hand it over to tee. So maybe you are better off to separate both, ie. redirect output in the known way and check the exit status of the command producing and have a look at the written log with tail -f for example.

Example:
Code:
make all >> make_all.out 2>&1; echo $?

# in another terminal session:
tail -f make_all.out

# 3  
Old 12-05-2008
from USENET comp.unix.shell FAQ:
Code:
======================================================================

13. How do I get the exit code of cmd1 in cmd1|cmd2

    First, note that cmd1 exit code could be non-zero and still don't
    mean an error. This happens for instance in

    cmd | head -1

    you might observe a 141 (or 269 with ksh93) exit status of cmd1,
    but it's because cmd was interrupted by a SIGPIPE signal when
    "head -1" terminated after having read one line.

    To know the exit status of the elements of a pipeline
    cmd1 | cmd2 | cmd3

    a. with zsh:

       The exit codes are provided in the pipestatus special array.
       cmd1 exit code is in $pipestatus[1], cmd3 exit code in
       $pipestatus[3], so that $? is always the same as
       $pipestatus[-1].

    b. with bash:

       The exit codes are provided in the PIPESTATUS special array.
       cmd1 exit code is in ${PIPESTATUS[0]}, cmd3 exit code in
       ${PIPESTATUS[2]}, so that $? is always the same as
       ${PIPESTATUS: -1}.

    c. with any other Bourne like shells

       You need to use a trick to pass the exit codes to the main
       shell.  You can do it using a pipe(2). Instead of running
       "cmd1", you run "cmd1; echo $?" and make sure $? makes it way
       to the shell.

       exec 3>&1
       eval `
         # now, inside the `...`, fd4 goes to the pipe
         # whose other end is read and passed to eval;
         # fd1 is the normal standard output preserved
         # the line before with exec 3>&1
         exec 4>&1 >&3 3>&- 
         {
           cmd1 4>&-; echo "ec1=$?;" >&4
         } | {
           cmd2 4>&-; echo "ec2=$?;" >&4
         } | cmd3
         echo "ec3=$?;" >&4
       `

    d. with a POSIX shell

       You can use this function to make it easier:

       run() {
         j=1
         while eval "\${pipestatus_$j+:} false"; do
           unset pipestatus_$j
           j=$(($j+1))
         done
         j=1 com= k=1 l=
         for a; do
           if [ "x$a" = 'x|' ]; then
             com="$com { $l "'3>&-
                         echo "pipestatus_'$j'=$?" >&3
                       } 4>&- |'
             j=$(($j+1)) l=
           else
             l="$l \"\$$k\""
           fi
           k=$(($k+1))
         done
         com="$com $l"' 3>&- >&4 4>&-
                    echo "pipestatus_'$j'=$?"'
         exec 4>&1
         eval "$(exec 3>&1; eval "$com")"
         exec 4>&-
         j=1
         while eval "\${pipestatus_$j+:} false"; do
           eval "[ \$pipestatus_$j -eq 0 ]" || return 1
           j=$(($j+1))
         done
         return 0
       }
       
       use it as:
       
       run cmd1 \| cmd2 \| cmd3
       exit codes are in $pipestatus_1, $pipestatus_2, $pipestatus_3

======================================================================


|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Clear contents of specified directories, then return exit status YouNicks Shell Programming and Scripting 6 08-27-2016 03:44 AM
Help with finding the exit status of a 'nohup' command using 'PID'. SriRamKrish Shell Programming and Scripting 4 09-18-2014 11:54 AM
Exit Status of Command pvmanikandan Shell Programming and Scripting 1 12-26-2013 01:25 PM
Exit Status Of Find Command Ariean UNIX for Dummies Questions & Answers 6 06-04-2013 09:17 PM
Equivalents of tee command to find exit status of command vibhor_agarwali UNIX for Advanced & Expert Users 7 06-03-2011 02:16 AM
Logins command exit status is 236 not 0 parkea2 HP-UX 2 11-27-2009 05:16 AM
Move Command and exit status problem visingha Shell Programming and Scripting 10 09-13-2008 09:08 PM
Move Command and exit status problem visingha UNIX for Dummies Questions & Answers 1 09-12-2008 03:14 AM
Return of EXIT status ( $? ) ZINGARO HP-UX 1 03-12-2008 08:07 AM
exit status of command in a pipe line topcat8 UNIX for Dummies Questions & Answers 10 10-19-2007 05:39 AM
Getting the exit status of a remote command zoonalex Shell Programming and Scripting 1 08-23-2006 05:58 PM
how to find the exit status for the last executed command vijay.amirthraj UNIX for Dummies Questions & Answers 1 07-04-2006 11:14 PM
Move command return with exit code of 2 handak9 UNIX for Advanced & Expert Users 1 08-26-2004 06:40 AM
Incorrect Exit Status Returned from FTP command - Help?? frustrated1 Shell Programming and Scripting 3 08-22-2003 04:25 AM
How to find the exit status of last command in Unix? rajugp1 Programming 1 09-10-2002 12:52 PM