Obtain return code before pipe 

Newsgroups:  comp.unix.shell
Date:        Tue, 05 Apr 2005 15:04:01 -0400
> I'm wondering if this is possible, i.e., to obtain return code before
> the pipe command. E.g., I want to do something like this
>
> real_command | grep -v garbage || error_handling command
>
> I want the error_handling to kick in only if the real_command fails, but
> apparently the above won't work because it takes the return code of grep,
> not the real_command.

If you use bash, you can use the $PIPESTATUS[] array variable.

real_command | grep -v garbage
if [[ ${PIPESTATUS[0]} -ne 0 ]]
then
  error_handling command
fi

Barry Margolin

Obtain return code before pipe 

> real_command | grep -v garbage || error_handling command

Would this suit you…?

if  real_command
then  :
else  error_handling command  1>&2
fi | grep -v garbage

Janis Papanagnou

Obtain return code before pipe 

> I'm wondering if this is possible, i.e., to obtain return code before
> the pipe command. E.g., I want to do something like this

See question 11 in the FAQ -

How do I get the exit code of cmd1 in cmd1|cmd2
http://home.comcast.net/~j.p.h/cus-faq.html#M

Ed Morton