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

Re: Redirecting shell output to a pipe



On Wed, 25 Nov 2009 08:48:01 +0200
"Nadav Har'El" <nyh@xxxxxxxxxxxxxxxxxxx> wrote:
> On Tue, Nov 24, 2009, Peter Miller wrote about "Re: Redirecting shell output to a pipe":
> >...
> > > Isn't it natural to assume that in the same fashion, you should also
> > > be able to redirect the scripts output to a pipe? E.g., a very useful
> > > idiom could have been
> > >
> > > 	exec | tee filename
> > 
> > exec |& tee filename
> 
> Does this work for you? I get (on zsh 4.3.10)
> 
> 	zsh: redirection with no command
> 
> This was exactly my "complaint" - that this is sensible syntax, that
> could have worked, but doesn't.

I think this does the wrong thing: it uses NULLCMD, i.e.  executes "exec
cat | tee filename" (or otherwise, depending what $NULLCMD is), which isn't
what you want.  If you don't use a NULLCMD this fails entirely.  Adding
2>&1 doesn't change this.

Actually, I don't think this would ever work.  You need to understand what
a pipeline means to the shell: it means create a pipe, then start the
different parts of the pipeline, then wait for them to finish.  What you
want is beyond the limits of expression of the standard pipeline syntax:
you want it to say create this pipeline and leave it running as a separate
process with output redirected.

Zsh does have a syntax for exactly that:

exec > >(tee filename)

That might do what you want.

Note the "tee" process is asynchronous with respect to the rest of the
script, i.e. the shell won't wait for it to exit before the script exits.
That's normally the case with pipelines; the second process waits for the
first to exit.  I'm noting it specially here because it's perhaps less
clear that the shell is becoming the first part of a pipeline over which it
has no overall control, i.e. that it's different from running a pipeline
entirely within the shell.  But it may already be obvious that's the case.

-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



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