From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 610 invoked from network); 16 Aug 2004 02:44:49 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 16 Aug 2004 02:44:49 -0000 Received: (qmail 72928 invoked from network); 16 Aug 2004 02:44:43 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 16 Aug 2004 02:44:43 -0000 Received: (qmail 3020 invoked by alias); 16 Aug 2004 02:44:00 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 7855 Received: (qmail 3007 invoked from network); 16 Aug 2004 02:43:59 -0000 Received: from unknown (HELO a.mx.sunsite.dk) (130.225.247.88) by 130.225.247.90 with SMTP; 16 Aug 2004 02:43:59 -0000 Received: (qmail 70895 invoked from network); 16 Aug 2004 02:42:29 -0000 Received: from unknown (HELO moonbase.zanshin.com) (167.160.213.139) by a.mx.sunsite.dk with SMTP; 16 Aug 2004 02:42:26 -0000 Received: from toltec.zanshin.com (toltec.zanshin.com [64.84.47.166]) by moonbase.zanshin.com (8.12.11/8.12.11) with ESMTP id i7G2gPxK006576 for ; Sun, 15 Aug 2004 19:42:25 -0700 Date: Sun, 15 Aug 2004 19:42:25 -0700 (PDT) From: Bart Schaefer Reply-To: zsh-users@sunsite.dk To: zsh-users@sunsite.dk Subject: Re: Slightly OT: Error-Handling in a Pipeline, preferably non-zsh In-Reply-To: <2FF1BBB1-EF20-11D8-9C9B-000A95EDC31A@louisville.edu> Message-ID: References: <2FF1BBB1-EF20-11D8-9C9B-000A95EDC31A@louisville.edu> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Spam-Checker-Version: SpamAssassin 2.63 on a.mx.sunsite.dk X-Spam-Level: X-Spam-Status: No, hits=-0.0 required=6.0 tests=BAYES_44 autolearn=no version=2.63 X-Spam-Hits: -0.0 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.