From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 9133 invoked from network); 16 Aug 2004 03:33:06 -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 03:33:06 -0000 Received: (qmail 3741 invoked from network); 16 Aug 2004 03:33:00 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 16 Aug 2004 03:33:00 -0000 Received: (qmail 12116 invoked by alias); 16 Aug 2004 03:32:16 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 7857 Received: (qmail 12104 invoked from network); 16 Aug 2004 03:32:16 -0000 Received: from unknown (HELO a.mx.sunsite.dk) (130.225.247.88) by 130.225.247.90 with SMTP; 16 Aug 2004 03:32:16 -0000 Received: (qmail 2403 invoked from network); 16 Aug 2004 03:30:39 -0000 Received: from sccimhc91.asp.att.net (63.240.76.165) by a.mx.sunsite.dk with SMTP; 16 Aug 2004 03:30:37 -0000 Received: from louisville.edu (12-220-223-80.client.insightbb.com[12.220.223.80]) by sccimhc91.asp.att.net (sccimhc91) with SMTP id <20040816033035i9100bube8e>; Mon, 16 Aug 2004 03:30:36 +0000 Date: Sun, 15 Aug 2004 23:30:33 -0400 Subject: Re: Slightly OT: Error-Handling in a Pipeline, preferably non-zsh Content-Type: text/plain; charset=US-ASCII; format=flowed Mime-Version: 1.0 (Apple Message framework v553) From: Aaron Davies To: zsh-users@sunsite.dk Content-Transfer-Encoding: 7bit In-Reply-To: <87n00v4we5.fsf@ceramic.fifi.org> Message-Id: X-Mailer: Apple Mail (2.553) X-Spam-Checker-Version: SpamAssassin 2.63 on a.mx.sunsite.dk X-Spam-Level: ** X-Spam-Status: No, hits=2.5 required=6.0 tests=BAYES_40,FROM_ENDS_IN_NUMS, RCVD_IN_SORBS autolearn=no version=2.63 X-Spam-Hits: 2.5 On Sunday, August 15, 2004, at 11:03 PM, Philippe Troin wrote: > Aaron Davies writes: > >> How do I do return-value error handling in the middle of a pipeline? >> I'd ideally like to keep this to as basic a shell level as possible, >> plain (Bourne) sh-compatible if it can be done, though a bash or zsh >> solution will be fine if not. I'm tring to write a simple script that >> will apply a command to all processes matching a name--sort of a >> generalized "killall". At the moment, it looks like this: >> >> #!/bin/sh >> >> name=$1 >> shift >> >> ps aux | grep $name | grep -v grep | grep -v $0 | awk '{ print $2 }' | >> xargs $@ >> >> and it works fine, and I'd like to keep it at that level of >> simplicity. The only thing is, I'd like to make it stop and return 1 >> if there are no matching processes. (At the moment, it calls the >> command with an empty argument list.) The intuitive thing to do seems >> to be >> >> ps aux | grep $name | grep -v grep | ( grep -v $0 || exit 1 ) | awk '{ >> print $2 }' | xargs $@ > > Use your first idiom and check $pipestatus[4]. Thanks, that seems to solve it (though it's [3], not [4]). I've combined that with xargs's "-r" option, which aborts if there are no arguments passed, to produce the following as the final script: #!/bin/sh name=$1 shift ps aux | grep $name | grep -v grep | grep -v $0 | awk '{ print $2 }' | xargs -r "$@" test ${PIPESTATUS[3]} -eq 0 && exit || exit 1 -- __ __ / ) / ) /--/ __. .__ ______ / / __. , __o _ _ / (_(_/|_/ (_(_) / (_ (__/_(_/|_\/ <__