Csh: Difference between revisions

From miki
Jump to navigation Jump to search
(Created page with '== Tips / How-To == === Get status of a pipe === In CSH (and Bash), <code>$status</code> returns the status of the last process in a pipe (actually this is only true for Bash; fo…')
 
Line 12: Line 12:
status=`((ls /FOO ; echo $? > &4) | sed 1,1d 1>&3) 4>&1`
status=`((ls /FOO ; echo $? > &4) | sed 1,1d 1>&3) 4>&1`
exit $status
exit $status
</source>

Another variant for ''ksh'' here (from [http://www.unix.com/unix-dummies-questions-answers/13018-exit-status-command-pipe-line.html]):
<source lang=bash>
#! /usr/bin/ksh
exec 4>&1
tail -5 >&4 |&
exec >&p
cat /etc/passwd
exitcode=$?
exec >&- >&4
wait
echo exitcode = $exitcode
exit 0
</source>
</source>

Revision as of 00:33, 14 March 2013

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