Csh

From miki
Jump to navigation Jump to search

Tips / How-To

Get status of a pipe

In CSH (and Bash), $status returns the status of the last process in a pipe (actually this is only true for Bash; for csh, it returns the status of the process that exited last!)

foo | bar
echo $status           # Print status of bar, not foo!

To still get the status of process foo, one has to use extra named pipe to carry on the information (see [1])

exec 3>&1 
status=`((ls /FOO ; echo $? > &4) | sed 1,1d 1>&3) 4>&1` 
exit $status

Another variant for ksh here (from [2]):

#! /usr/bin/ksh
exec 4>&1
tail -5 >&4 |&
exec >&p
cat /etc/passwd
exitcode=$?
exec >&- >&4
wait
echo exitcode = $exitcode
exit 0