Bash help requested: Capturing command errors within pipes

Daniel B. Thurman dant at cdkkt.com
Thu Mar 19 19:19:00 UTC 2009


Steven W. Orr wrote:
> On Thursday, Mar 19th 2009 at 14:11 -0000, quoth Daniel B. Thurman:
>
> =>S P Arif Sahari Wibowo wrote:
> =>> On Thu, 19 Mar 2009, Daniel B. Thurman wrote:
> =>> > How could one go about capturing command errors within pipes?
> =>> 
> =>> The variable $PIPESTATUS is just for that.
> =>> 
> =>I tried it, seems not to work.
> =>
> =>Try capturing a sed error when re1 or re2 has a bad regular
> =>expression string.  I have not been able to catch the error.
>
> Sorry, but you'll have to supply something more specific. It's possible 
> for a program to have an error but not return a proper exit status.
>
> But so far, my highly scientific test has revealed that all is good in 
> pipe testing land in the Kingdom of Bash.
>
> 578 > true | false | true | true
> 579 > echo ${PIPESTATUS[@]}
> 0 1 0 0
>   

OK, I found out why I had a problem!

I setup an intentional sed error:
$ (re="-e '\'s/b/h/'"; echo "boo" | sed $re | echo "done"; echo 
${PIPESTATUS})
done
sed: -e expression #1, char 1: unknown command: `''
0

Same thing but with @ pipe status expression
$ (re="-e '\'s/b/h/'"; echo "boo" | sed $re | echo "done"; echo 
${PIPESTATUS[@]})
done
sed: -e expression #1, char 1: unknown command: `''
0 1 0

My problem was that the last command was OK, of which
PIPESTATUS was reporting, thus I was not properly capturing
the exact status location where the error occurred!  duh!

Your example showed me the way!

Thanks!
Dan




More information about the fedora-list mailing list