Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: Slightly OT: Error-Handling in a Pipeline, preferably non-zsh
- X-seq: zsh-users 7855
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxxxxx
- Subject: Re: Slightly OT: Error-Handling in a Pipeline, preferably non-zsh
- Date: Sun, 15 Aug 2004 19:42:25 -0700 (PDT)
- In-reply-to: <2FF1BBB1-EF20-11D8-9C9B-000A95EDC31A@xxxxxxxxxxxxxx>
- Mailing-list: contact zsh-users-help@xxxxxxxxxx; run by ezmlm
- References: <2FF1BBB1-EF20-11D8-9C9B-000A95EDC31A@xxxxxxxxxxxxxx>
- Reply-to: zsh-users@xxxxxxxxxx
On Sun, 15 Aug 2004, Aaron Davies wrote:
> How do I do return-value error handling in the middle of a pipeline?
The trouble is that you're not asking for return-value error handling,
you're asking for conditional execution. That is, you want a pipeline
that acts like an && conjunction. This just can't be; in order to set up
all the stdout-to-stdin connections between the processes in the pipeline,
the controlling shell must start them up without knowing whether any one
of them is (or isn't) actually going to produce (or consume) any output.
The closest you could get would be to use a named pipe:
mkfifo psgrep
{ ps aux | grep $name | grep -v grep | grep -v $0; } > psgrep &&
{ awk '{ print $2 }' | xargs $@; } < psgrep
rm psgrep
However, that's subject to deadlock if the the first pipeline produces
more output before it exits than can be buffered in the fifo, and except
for the mkfifo it looks exactly the same as the temp file solution you
already suggested.
There is no reliable way to do this without capturing the first part of
the output somewhere. Instead of using a temp file, you could capture in
a variable:
psgrep="`ps aux | grep $name | grep -v grep | grep -v $0`"
[ -n "$psgrep" ] && { echo "$psgrep" | awk '{ print $2 }' | xargs $@; }
Or you could skip the awk and xargs entirely and use a while loop:
ps aux | grep $name | grep -v grep | grep -v $0 |
while read user pid remainder
do
$@ $pid
done
As a final note, you probably want "$@" in double quotes.
Messages sorted by:
Reverse Date,
Date,
Thread,
Author