zsh-users
 help / color / mirror / code / Atom feed
* long pipelines and coprocesses
@ 1999-02-18  0:48 Oliver Kiddle
  1999-02-18  8:45 ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Oliver Kiddle @ 1999-02-18  0:48 UTC (permalink / raw)
  To: Zsh Users List

In a shell script I'm writing, I've got an enormous pipe of commands and
I've used the following construct as one of the commands in the pipe:

( ( [ "$dogrep" ] && grep "$1" ) || cat - ) | \

Does anyone know of a more efficient way of doing this (i.e. without the
cat -) other than having an outer if statement and repeating the whole
pipeline of commands.

What do I have to do to get coprocesses working in zsh scripts. They
aren't the best documented feature.

I have the following test script:

-----
while read file; do
  rm $file
done |&

:>testfile
ls
print -p testfile
ls
-----

It seems to work fine in ksh but in zsh it just hangs. I've tried it on
both AIX and IRIX.

Thanks

Oliver Kiddle


^ permalink raw reply	[flat|nested] 2+ messages in thread

* long pipelines and coprocesses
  1999-02-18  0:48 long pipelines and coprocesses Oliver Kiddle
@ 1999-02-18  8:45 ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 1999-02-18  8:45 UTC (permalink / raw)
  To: Oliver Kiddle; +Cc: Zsh Users List

Oliver Kiddle writes:
 > ( ( [ "$dogrep" ] && grep "$1" ) || cat - ) | \
 > 
 > Does anyone know of a more efficient way of doing this (i.e. without the
 > cat -) other than having an outer if statement and repeating the whole
 > pipeline of commands.

Sure.

function tail_of_the_pipeline { blah | blah | blah }
[[ whatever ]] &&
    { grep $1 | tail_of_the_pipeline "$@" } ||
    tail_of_the_pipeline "$@"

Unlike other shells that fork the right-hand-side of pipelines, zsh forks
the left sides, so parameter changes that occur in tail_of_the_pipeline
can affect the current shell.  It's amazingly difficult to write bash
scripts that accomplish the equivalent.

All of the locals declared in the outer function/script are visible as
"globals" inside tail_of_the_pipeline, so as long as you remeber to declare
them (and not to unnecessarily redeclare them inside tail_of_the_pipeline)
you should just be able to move whatever code you've already written into
the new function, otherwise unchanged.

Of course, I'm not sure that's really what you mean, as in this example the
tail of the pipeline will run a second time if the grep fails.  (In your
original example, the "cat -" will run if the grep fails.)  You probably
don't want to use the && and || syntax at all here; rather,

      if [[ whatever ]]
      then grep $1 | tail_of_the_pipeline "$@"
      else tail_of_the_pipeline "$@"
      fi

 > What do I have to do to get coprocesses working in zsh scripts. They
 > aren't the best documented feature.

I think the coprocess documentation has been improved in 3.1.5, but it
probably could still use a lot of work.

 > I have the following test script:
 > 
 > -----
 > while read file; do
 >   rm $file
 > done |&

Zsh doesn't use the |& syntax for creating coprocesses; that syntax is
already taken as a shorthand for 2>&1|.  This is probably the most
significant incompatibility between ksh and zsh-emulating-ksh.  What you
want is

    coproc { while read file; do rm $file; done }

There are also some other different things about zsh's coproc; for example,
you can't use the same file descriptor tricks to close the input or output
of a zsh coprocess that you might use in ksh.  (This may have changed a
little in 3.1.5, I've forgotten.)


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1999-02-18  8:48 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-02-18  0:48 long pipelines and coprocesses Oliver Kiddle
1999-02-18  8:45 ` Bart Schaefer

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).