zsh-users
 help / color / mirror / code / Atom feed
From: Aaron Davies <agdavi01@louisville.edu>
To: zsh-users@sunsite.dk
Subject: Slightly OT: Error-Handling in a Pipeline, preferably non-zsh
Date: Sun, 15 Aug 2004 21:04:12 -0400	[thread overview]
Message-ID: <2FF1BBB1-EF20-11D8-9C9B-000A95EDC31A@louisville.edu> (raw)

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 $@

but that doesn't work. Any ideas? (I could, of course, simply cache the 
last grep's results in a tmpfile, test its length, and proceed 
accordingly, but that doesn't seem quite as elegant.
-- 
Aaron Davies
agdavi01@louisville.edu


             reply	other threads:[~2004-08-16  1:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-16  1:04 Aaron Davies [this message]
2004-08-16  2:42 ` Bart Schaefer
2004-08-16  3:33   ` Aaron Davies
2004-08-16  7:41     ` Bart Schaefer
2004-08-16 12:40       ` Aaron Davies
2004-08-16 14:53         ` DervishD
2004-08-17  3:06           ` Bart Schaefer
2004-08-16  3:03 ` Philippe Troin
2004-08-16  3:30   ` Aaron Davies
2004-08-16  7:51     ` Bart Schaefer
2004-08-16 12:41       ` Aaron Davies
2004-08-16 16:08         ` Dan Nelson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2FF1BBB1-EF20-11D8-9C9B-000A95EDC31A@louisville.edu \
    --to=agdavi01@louisville.edu \
    --cc=zsh-users@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).