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

Re: PATCH: allocating a new file descriptor



Bart Schaefer wrote:
> For example,
> 
>     print foo {myfd}>/dev/null
> 
> assigns a new fd to $myfd, but does not redirect the output of "print"
> to it, and leaves it open after "print" is finished.
> 
> Futher, executing the same command again assigns a second new fd to $myfd
> but leaves the first one "dangling" (no way to close it, because you no
> longer know what its number is).

That's just how it works.  It's not supposed to redirect print to the
new fd, it's specifically a syntax to allocate an fd only, and it's
quite explicit that the number is stored in the variable, so if you want
to close it you keep the variable around.  It's only a bug if you
confuse allocation and closing of fd's with their use (which doesn't
mean people won't, nor not neither using double negatives).

I can add documentation to point this out if you think it would be
useful.

It would also be possible to make NO_CLOBBER check that in {myfd}>stuff,
$myfd doesn't already point to an fd with an FDT_EXTERNAL flag.  That
would prevent overwriting.  I quite like that idea.  You could still
loop:

  for file in $filelist; do
     exec {myfd}>$file

     do_stuff_with >&$myfd

     exec {myfd}>&-
  done

because the test wouldn't trigger after $myfd was closed.  For more
complicated cases you might need to set myfd to 0 first (fd's below 10
are never marked with the flag).  Obviously NO_CLOBBER wouldn't apply to
closing the fd.

I don't think there's a good argument for adding automatic redirection,
though.  Fortuitously,

  print This is a log file {myfd}>~/tmp/logfile.txt >&$myfd

works; $myfd is substituted at the right point.  That's quite lucky,
since most expansions have been done by this point.

However, there's also the point that if you use this after an external
command the shell has already forked and the fd doesn't appear in the
parent shell.  Allocation of fd's is most naturally done after "exec"
only.

-- 
Peter Stephenson <pws@xxxxxxx>                  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.

**********************************************************************



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