On Friday 26 Mar 2010, Logu wrote:
> 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"

Two problems here:

1. The whole pipe executes in a subshell, so variables set in the pipe 
will not be visible in the calling shell.

2. The problem you have noted, viz getting the exit status of the first 
command in the pipe.

> 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.

The second seems to be the best way of doing it.  If you're sure there's 
nothing else being printed to STDERR, you could also output the exit 
status to STDERR and capture it later.  Untested, but something like 
this could work:

stat=`(( cat $file; echo $? >&2 ) | sed... > $anotherfile) 2>&1`

Of course, that will also trap error messages from any and all commands 
in the pipe, so YMMV.

Regards,

-- Raju
-- 
Raj Mathur                r...@kandalaya.org      http://kandalaya.org/
       GPG: 78D4 FC67 367F 40E2 0DD5  0FEF C968 D0EF CC68 D17F
PsyTrance & Chill: http://schizoid.in/   ||   It is the mind that moves

------------------------------------------------------------------------------
Download Intel® 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
_______________________________________________
linux-india-help mailing list
linux-india-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-india-help

Reply via email to