Email address obfuscation in effect -- please
click here to turn it off.
[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
Mark Rages inquired:
>
>> cmd1 | cmd2 | grep SOMETHING >/dev/null 2>&1
>
> In this example, I would want to know if "cmd1" failed with an error.
No can do as a pipeline in bash.
>From 'man bash':
> Pipelines
> A pipeline is a sequence of one or more commands separated by the character |.
> The format for a pipeline is:
>
> [time [-p]] [ ! ] command [ | command2 ... ]
>
> The standard output of command is connected via a pipe to the standard input
> of command2. This connection is performed before any redirections specified
> by the command (see REDIRECTION below).
>
> If the reserved word ! precedes a pipeline, the exit status of that pipeline
> is the logical NOT of the exit status of the last command. Otherwise, the
> status of the pipeline is the exit status of the last command. The shell
> waits for all commands in the pipeline to terminate before returning a value.
In short, there is no such thing as "the exit code for cmd1" in a pipeline.
You'll need to explicitly use subshells and test exit codes.
Think about it. It's a pipeline. cmd2 is initiated PRIOR to the end of cmd1.
Therefore cmd1's exit status is irrelevant. If you need to know whether cmd1
was successful or not, you need to have COMPLETED cmd1.
The pipeline behaves in a serial piping metaphor, not a parallel operation
optimization metaphor (e.g. a CPU instruction pipeline).
One way to handle the problem, however, is through the use of signals, i.e.
exception handling.
Alternatively, implement cmd1, cmd2, etc. in a programming language that
supports manipulating system signals (such as Perl or C/C++) if you have
to use or cannot re-write external binaries, or a language that supports
exception handling programmatically (such as Perl or Java or C/C++).
Perl's equivalent to Java's try/catch/throw is eval/$!/die or use
an exception handling module from CPAN (Class::Throwable, Exception).
See http://search.cpan.org/search?query=exception&mode=all for more.
Mike/
_______________________________________________
members mailing list
EMAIL:PROTECTED
http://mlug.missouri.edu/mailman/listinfo/members