Unix Shell - how to detect an error in a bash pipe?

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > March 2005 > how to detect an error in a bash pipe?





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author how to detect an error in a bash pipe?
Phony Account

2005-03-29, 8:48 pm

Hi,

if I have a pipe consisting of several commands, and one of them returns
with exit -ne 0, how can I detect that?

I tried the [ $? ] test (if I remember that command correctly) but that
did not seem to work.

Thanks,

Mirko
Chris F.A. Johnson

2005-03-29, 8:48 pm

On Wed, 30 Mar 2005 at 02:43 GMT, Phony Account wrote:
> Hi,
>
> if I have a pipe consisting of several commands, and one of them returns
> with exit -ne 0, how can I detect that?
>
> I tried the [ $? ] test (if I remember that command correctly) but that
> did not seem to work.


Bash sets an array, PIPESTATUS, "containing a list of exit status
values from the processes in the most-recently-executed foreground
pipeline (which may contain only a single command)."

$ ls q w e r t 2>&1 | cat | grep x
$ printf "%s\n" "${PIPESTATUS[@]}"
1
0
1


--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
========================================
===========================
My code (if any) in this post is copyright 2005, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License
Ed Morton

2005-03-30, 2:50 am



Phony Account wrote:

> Hi,
>
> if I have a pipe consisting of several commands, and one of them returns
> with exit -ne 0, how can I detect that?
>
> I tried the [ $? ] test (if I remember that command correctly) but that
> did not seem to work.


See question 13 in the FAQ (http://home.comcast.net/~j.p.h/cus-faq.html#M).

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com