[LIH] Getting the exit status of piped command

Logu <[email protected]>
Newsgroups gmane.user-groups.linux.india.help
Message-ID <[email protected]>
Hi,

I am trying to get the exit status of the first command on the pipe,  
but I always get the exit status of the pipe reader.

     cat nofile; |sed -n '/abc/p'; echo "Error: $?"

I tried this, but didnt work to my requirement

     { cat nofile; err=$?; } |sed -n '/abc/p'; echo "Error: $err"

This is actually a simplified version of my requirement, any other  
method to accomplish this efficiently?

Two possible solution are
1. Find the exit status first by executing the cat command and then do  
the pipe again.
     Problem: I actually have a command in place of cat which takes  
long time and hence I can not execute it twice.

2. write the exit status to a file and use it further.
    Problem: some how I feel there is a better way of doing without  
writing the value to the disk.

Any other solutions.

Regards
-logu


------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.