zsh-users
 help / color / mirror / code / Atom feed
* exec | wc isn't very useful...
@ 2004-12-15 18:59 Anthony Heading
  2004-12-15 19:18 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Anthony Heading @ 2004-12-15 18:59 UTC (permalink / raw)
  To: zsh-users


Hi,

Can anyone tell me why this doesn't do what I'd hope:

    exec | wc

I'm wondering how to redirect the shell's "default" stdout into
a process, e.g. do the equivalent of perl's

    open(STDOUT, "| wc");
    print "two words";

Obviously one can do "echo two words | wc", or "wc <(echo two words)"
or whatever, but that doesn't seem to lend itself too well to
conditional redirection, viz:

    case $out in
	script)  exec > script.out ;;
	gnuplot) exec | gnuplot ;;  # doesn't work - hmm
	stdout) ;;
    esac

Equivalently, a sometimes appealing alternative to:

    (
	echo foo
	echo bar
    ) > xyzzy.txt

is

    exec > xyzzy.txt
    echo foo
    echo bar

but this doesn't seem to work for pipelines.  Except that
perl can do it no trouble...

Pointers to anything I'm missing very welcome...

Rgds

Anthony

This communication is for informational purposes only.  It is not intended
as an offer or solicitation for the purchase or sale of any financial
instrument or as an official confirmation of any transaction. All market prices,
data and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein 
do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries 
and affiliates


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

* Re: exec | wc isn't very useful...
  2004-12-15 18:59 exec | wc isn't very useful Anthony Heading
@ 2004-12-15 19:18 ` Peter Stephenson
  2004-12-15 22:51   ` Anthony Heading
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2004-12-15 19:18 UTC (permalink / raw)
  To: zsh-users

Anthony Heading wrote:
> 
> Hi,
> 
> Can anyone tell me why this doesn't do what I'd hope:
> 
>     exec | wc

The syntax doesn't make sense.  You're piping the output of "exec" on
its own, which doesn't do anything, to "wc".  A pipeline is
fundamentally different from a redirection.

> I'm wondering how to redirect the shell's "default" stdout into
> a process, e.g. do the equivalent of perl's
> 
>     open(STDOUT, "| wc");
>     print "two words";

Try this:

exec > >(wc)

Something like this got broken at one point (by me) but is working in
the latest version of the shell.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

* Re: exec | wc isn't very useful...
  2004-12-15 19:18 ` Peter Stephenson
@ 2004-12-15 22:51   ` Anthony Heading
  2004-12-16 13:50     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Anthony Heading @ 2004-12-15 22:51 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users


On Wed, Dec 15, 2004 at 07:18:00PM +0000, Peter Stephenson wrote:
> >     exec | wc
> 
> The syntax doesn't make sense.  You're piping the output of "exec" on
> its own, which doesn't do anything, to "wc". 

Yes... doesn't that just mean that it's free to be assigned any
meaning that an implementer might care to give it?

> Try this:
> exec > >(wc)
> Something like this got broken at one point (by me) but is working in
> the latest version of the shell.

Ah.  Perfect.  I'm sure tried that before and it didn't work -
perhaps I hit the period when it was broken...

> A pipeline is fundamentally different from a redirection.

I don't quite see that - aside from the very real underlying
implementation issues, what is wrong with a conceptual model
that views:
    a | b | c
as pure syntactic sugar for:
    a > >(b > >(c))
?

Would it not be appealing to _define_
    exec | wc
to mean:
    exec > >(wc)

given that it doesn't currently mean anything at all?

Regardless, appreciate your help with identifying a working
syntax...

Rgds

Anthony

This communication is for informational purposes only.  It is not intended
as an offer or solicitation for the purchase or sale of any financial
instrument or as an official confirmation of any transaction. All market prices,
data and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein 
do not necessarily reflect those of JPMorgan Chase & Co., its subsidiaries 
and affiliates


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

* Re: exec | wc isn't very useful...
  2004-12-15 22:51   ` Anthony Heading
@ 2004-12-16 13:50     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2004-12-16 13:50 UTC (permalink / raw)
  To: zsh-users

Anthony Heading wrote:
> > A pipeline is fundamentally different from a redirection.
> 
> I don't quite see that - aside from the very real underlying
> implementation issues, what is wrong with a conceptual model
> that views:
>     a | b | c
> as pure syntactic sugar for:
>     a > >(b > >(c))
> ?

Currently, the combination > >(...) is not special; >(...) produces a
file name (either a named pipe or a /dev/fd entry) and > redirects into
it.  In principle that could be changed to use pipes internally.

There are still some major differences, however.

First, a > >(b) creates b as a subprocess and then runs a.  This is
different from a | b as here a is created in a subprocess, then b is
run.  Actually, some shells do it the other way around, but there are
some good uses for having b running in the main shell.

Second, the shell doesn't wait for the processes in the command
substitution to exit.  This is different from a pipeline where the shell
waits for the all the processes before the job is finished.

A more subtle, but possibly the most critical, difference is that in
a > >(b > >(c))
the process c is forked from b, not from the parent shell.  This means
that the parent shell isn't even aware of c.  This makes it impossible
to treat as a pipeline where all the processes are forked from (or run
in) the parent shell.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************


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

end of thread, other threads:[~2004-12-16 13:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-15 18:59 exec | wc isn't very useful Anthony Heading
2004-12-15 19:18 ` Peter Stephenson
2004-12-15 22:51   ` Anthony Heading
2004-12-16 13:50     ` Peter Stephenson

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