Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: coproc tutorial (Re: questions)



On Oct 3,  2:08pm, Chris Hansen wrote:
} Subject: Re: coproc tutorial (Re: questions)
}
} I now understan how to coproc, when is it actually the best way?
} What have you used it for?

To be perfectly honest, I've never used a coproc for anything except
figuring out how to explain what coprocs do.

I suspect they're rather more useful in other ksh-like shells because of
the handling of piping into shell constructs.  In zsh, a "while ...; do"
loop at the right-hand-end of a pipeline is run in the current shell 
(unless backgrounded), so you can set and export parameters and so on 
and they remain set when the loop finishes.  Zsh is unique in this; in
other shells, such a loop would be run in a subshell.

But with a coproc, you can start the left side of the "pipeline" in the
coprocess and then use redirection for the input of the loop, and thus
you get the loop run in the current shell.

Coprocs are also useful if you ever have two commands that each want to
consume the other's output:

    coproc command1
    command2 <&p >&p

I've never had reason to do that, but I suppose it would be good if you
were testing a pair of network protocol daemons.

The only other reason I can think of for using a coprocess would be to
avoid using an "eval".  Something like:

    if condition1; then
        coproc command1
    elif condition2; then
        coproc command2
    else
        coproc command3
    fi
    # ... do a bunch of other setup for command4, then ...
    command4 <&p

On Oct 3,  7:01pm, Chris Hansen wrote:
} Subject: coproc
}
} Is there a way to write to the keyboard of a coprocess?
} If not, how hard would this feature be to add?

I can't answer that, as I'm completely mystified as to what "the keyboard
of a coprocess" means.  A coprocess is a background job; to the extent
that it has "a keyboard" it would be the same one as the shell that had
started it.  Can you give an example of what you're trying to accomplish?

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com



Messages sorted by: Reverse Date, Date, Thread, Author