It is currently Tue, 30 Nov 2021 17:05:26 GMT



 
Author Message
 exit status of pipeline in sh/csh

In debugging a problem with popen, I uncovered a painful difference in
how C shell and Bourne Shell treat a pipeline.

Here is a simple Bourne Shell example:

$ ls /FOO | sed 1,1d
/FOO: No such file or directory
$ echo $?
0

The 'ls /FOO' commands fails with a non zero exit status since /FOO
doesn't exist. However, since the sed executes, the exit status is set
to 0. This is documented behavior according to the sh(1) man page.

In C shell, the same example sets the "correct" exit status:

% ls /FOO | sed 1,1d
/FOO: No such file or directory
% echo $status
2

Is there some way to make Bourne Shell behave like C Shell for the above
example?

In the actual program, I don't actually use 'ls /FOO', but need to have
sed or something similar strip the first line of output for any given
command.

I searched the FAQs, tried using &&, || and set -e, but am no closer to
a solution. I need a Bourne Shell solution since I am invoking the
command via popen, which invokes Bourne Shell. Also, I don't want to
introduce any substitution or other side effects by invoking popen with
"exec /bin/csh -c ..." as the argument.

Anybody have any ideas on how to solve this? Please mail your answers to
me and I'll summarize what I get and post the results.

Thanks!



 Mon, 17 Jul 2000 03:00:00 GMT   
 exit status of pipeline in sh/csh

The csh behavior is not predictable :
will give 1 (if foo doesn't exist on my linux) but
will give 0 (even if foo doesn't exist)

Well, for the solution with sh, you should read :
http://gonzo.tamu.edu/csh.whynot.html

Summary :

exec 3>&1
STATUS=`( ( command1 ; echo $? >&4 ) | command2 1>&3) 4>&1`

--
Stephane



 Tue, 18 Jul 2000 03:00:00 GMT   
 exit status of pipeline in sh/csh

In article <slrn6d4c1l.ds.Stephane.CHAZE...@pcchazelas.maisel.enst-bretagne.fr>,

I suspect the difference is because "echo" is a shell built-in but "cat"
isn't.

--
Barry Margolin, bar...@bbnplanet.com
GTE Internetworking, Powered by BBN, Cambridge, MA
Support the anti-spam movement; see <http://www.cauce.org/>
Please don't send technical questions directly to me, post them to newsgroups.



 Tue, 18 Jul 2000 03:00:00 GMT   
 exit status of pipeline in sh/csh

Stephane,

The solution you suggested works, i.e.

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

I'll have to wait till Monday to try it in the actual code with popen,
but it does what I need from the command line so I am sure it will work
in the code.

Thanks for the help and the pointer to the web page!



 Wed, 19 Jul 2000 03:00:00 GMT   
 
   [ 4 post ] 

Similar Threads

1. How to get exit status code in a pipeline


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software