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

Re: {fd}< and compound commands



On Wed, 14 Sep 2011 18:21:48 +0100
Stephane Chazelas <stephane_chazelas@xxxxxxxx> wrote:
> $ zsh -c '{echo foo >&$fd;}  {fd}> a'
> zsh:1: parse error near `{'
> (same for while/do/done constructs at least)
> 
> Is that the expected behavior? I can't see it being documented.

It is documented now.  This is all complicated stuff with wide
implications for the parser; there's is no chance I will have to time to
change it myself.

> That would be useful in
> 
> while read <&$fd var; do
>   ...
> done {fd}< file
> 
> to avoid having to worry about overriding fds 3-9.

There's no code automatically to close file descriptors anyway.
Generally, that syntax isn't doing quite what the use of redirection
syntax would suggest; it's permanently opening a file descriptor, not
performing a possibly temporary redirection. You need to do:

local fd

{
  exec {fd}<file

  while read; do
    ...
  done <&$fd
} always {
  exec {fd}<&-
}

(although in this particular case there's nothing to stop you doing

while read var; do
  ...
done <file

but I presume you have something more complicated in mind.)

-- 
Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/



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