On Mar 26, 2010, at 7:10 PM, Raj Mathur wrote:

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

Thanks for your mail. I got a solution atleast this works for bash
http://tldp.org/LDP/abs/html/internalvariables.html#PIPESTATUSREF

The exit status of the first command is stored on ${PIPESTATUS[0]}


Regards
-logu

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