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

Re: Redirecting shell output to a pipe



On Tuesday 24 November 2009 19:33:45 Peter Miller wrote:
> Nadav Har'El wrote:
> > Hi,
> >
> > If a shell script wants all its output to go to a file, it's easy:
> >
> > 	exec >filename 2>&1
> >
> > 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

bourne shell compatible syntax:

  exec 2>&1 | tee filename

> 
> > to redirect stdout both to a file, and to the terminal.
> > But unfortunately, the above line does *not* work. Is there a reason
> > why it doesn't? Wouldn't this be a useful feature?
> >
> > By the way, for the curious, there is actually a different solution
> > to this need, but it is much more convoluted. The other solution is
> > to write this:
> >
> > 	exec 3>&1
> > 	coproc tee /tmp/b >&3
> > 	exec >&p 2>&1
> >
> > (and perhaps do something, I'm not even sure what, at the end of the
> > script to give the tee coprocess enough time to finish outputting
> > before the script exits).
> >
> > Another downside of this solution, besides being extremely complex
> > (99% of the zsh users will probably not be able to figure it out), is
> > that it takes the only coprocess that zsh gives you, which won't work
> > if your code is already using coprocesses.
> 

Attachment: signature.asc
Description: This is a digitally signed message part.



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