zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: Slightly OT: Error-Handling in a Pipeline, preferably non-zsh
Date: Sun, 15 Aug 2004 19:42:25 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.61.0408151913400.935@toltec.zanshin.com> (raw)
In-Reply-To: <2FF1BBB1-EF20-11D8-9C9B-000A95EDC31A@louisville.edu>

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.


  reply	other threads:[~2004-08-16  2:44 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-08-16  1:04 Aaron Davies
2004-08-16  2:42 ` Bart Schaefer [this message]
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=Pine.LNX.4.61.0408151913400.935@toltec.zanshin.com \
    --to=schaefer@brasslantern.com \
    --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).