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…')
 
 
(One intermediate revision by the same user not shown)
Line 7: Line 7:
</source>
</source>


In ''Bash'', one can use array <code>PIPESTATUS</code> to get the status of all processes in a pipe.
To still get the status of process ''foo'', one has to use extra named pipe to carry on the information (see [http://www.linuxmisc.com/10-unix-questions/e2ae128f29acee80.htm])

On ''Csh'', one can also get the status of process ''foo'', but it requires an extra named pipe to carry on the information (see [http://www.linuxmisc.com/10-unix-questions/e2ae128f29acee80.htm])
<source lang=bash>
<source lang=bash>
exec 3>&1
exec 3>&1
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>

Latest revision as of 00:35, 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!

In Bash, one can use array PIPESTATUS to get the status of all processes in a pipe.

On Csh, one can also get the status of process foo, but it requires an 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