?ksh: exit status of 1st cmd in pipe?

?ksh: exit status of 1st cmd in pipe?

Post by Philip Shephe » Thu, 19 Aug 1993 14:13:22



Using ksh, I want to pipe several commands together, but be able to
determine the exit status of the first command in the pipe:

  #!/usr/bin/ksh

  firstcmd 2>&1 | tee /dev/console >>$logfile
  exit_status=$?

  if [ $exit_status = 0 ] ; then
     etc...

Using the above script, exit_status is set to the exit status of "tee".
However, I'm interested in the exit status of "firstcmd"

Any ideas?  I could of course spool the output of firstcmd to a temporary
file but then the console wouldn't see any error messages until "firstcmd"
exited ... not really what I want to happen.

Thanks in advance

Phil

--

University of New England
Northern Rivers
Lismore NSW Australia

 
 
 

?ksh: exit status of 1st cmd in pipe?

Post by Kjetil Torgrim Hom » Sat, 21 Aug 1993 12:35:14


Philip Sheperd:

Quote:> Using ksh, I want to pipe several commands together, but be able to
> determine the exit status of the first command in the pipe:

Generally, I don't think this is possible without temporary files.
Ie. something like
   (firstcmd; echo $? 9> /tmp/exit 1>&9) 2>&1 | secondcmd
   exit_status=`cat /tmp/exit`; rm -f /tmp/exit

Quote:>  firstcmd 2>&1 | tee /dev/console >>$logfile
>  exit_status=$?

If we use 'tail -f' instead of 'tee' we get:

   firstcmd 2>&1 >> $logfile &
   firstpid=$!

   tail -f $logfile &               # >/dev/console if you _insist_ :-)
   tailpid=$!

   wait $firstpid

   exit_status=$?
   kill $tailpid

BTW, tee is usually used this way: '| tee $logfile' - then stdout will
be stdout.

Kjetil T.

 
 
 

?ksh: exit status of 1st cmd in pipe?

Post by David W. Tamk » Sat, 21 Aug 1993 13:19:52




| Philip [Shepherd]:

| >  firstcmd 2>&1 | tee /dev/console >>$logfile

| BTW, tee is usually used this way: '| tee $logfile' - then stdout will
| be stdout.

tee -a $logfile

Note the append redirector in Philip's example.  A simple "tee $logfile"
will print to the console just fine but will clobber whatever was already
in $logfile.

David W. Tamkin  Box 3284  Skokie, Illinois  60076-6284  312-714-5610

 
 
 

?ksh: exit status of 1st cmd in pipe?

Post by Philip Shephe » Sat, 21 Aug 1993 15:21:46


My original question
:
:Using ksh, I want to pipe several commands together, but be able to
:determine the exit status of the first command in the pipe:
:
:  #!/usr/bin/ksh
:
:  firstcmd 2>&1 | tee /dev/console >>$logfile
:  exit_status=$?
:
:  if [ $exit_status = 0 ] ; then
:     etc...
:
:Using the above script, exit_status is set to the exit status of "tee".
:However, I'm interested in the exit status of "firstcmd"


Quote:> Tom Christiansen has an example of doing this in the Bourne shell (and
> since the ksh is generally backwards compatible with the Bourne shell,
> it should be a solution for you too), in his "Csh considered harmful"
> paper, available via anonymous ftp from convex.com:/pub/csh.whynot.

From which I derived:

   status=`(( first_command; echo $? >&3) | tee $terminal >>$logfile) 3>&1`

Thanks to jdavis for his quick response

--

University of New England
Northern Rivers
Lismore NSW Australia