zsh-users
 help / color / mirror / code / Atom feed
* {fd}< and compound commands
@ 2011-09-14 17:21 Stephane Chazelas
  2011-09-14 17:36 ` Mikael Magnusson
  2011-09-14 19:06 ` Peter Stephenson
  0 siblings, 2 replies; 10+ messages in thread
From: Stephane Chazelas @ 2011-09-14 17:21 UTC (permalink / raw)
  To: zsh-users

Hiya,

$ echo $ZSH_VERSION
4.3.12
$ 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.

That would be useful in

while read <&$fd var; do
  ...
done {fd}< file

to avoid having to worry about overriding fds 3-9.

-- 
Stephane


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

* Re: {fd}< and compound commands
  2011-09-14 17:21 {fd}< and compound commands Stephane Chazelas
@ 2011-09-14 17:36 ` Mikael Magnusson
  2011-09-14 19:06 ` Peter Stephenson
  1 sibling, 0 replies; 10+ messages in thread
From: Mikael Magnusson @ 2011-09-14 17:36 UTC (permalink / raw)
  To: zsh-users

On 14 September 2011 19:21, Stephane Chazelas
<stephane_chazelas@yahoo.fr> wrote:
> Hiya,
>
> $ echo $ZSH_VERSION
> 4.3.12
> $ 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.
>
> That would be useful in
>
> while read <&$fd var; do
>  ...
> done {fd}< file
>
> to avoid having to worry about overriding fds 3-9.

Is this related to http://www.zsh.org/mla/workers/2011/msg01090.html
(and reply from pws)?

-- 
Mikael Magnusson


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

* Re: {fd}< and compound commands
  2011-09-14 17:21 {fd}< and compound commands Stephane Chazelas
  2011-09-14 17:36 ` Mikael Magnusson
@ 2011-09-14 19:06 ` Peter Stephenson
  2011-09-14 19:31   ` Stephane Chazelas
                     ` (2 more replies)
  1 sibling, 3 replies; 10+ messages in thread
From: Peter Stephenson @ 2011-09-14 19:06 UTC (permalink / raw)
  To: zsh-users

On Wed, 14 Sep 2011 18:21:48 +0100
Stephane Chazelas <stephane_chazelas@yahoo.fr> 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@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


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

* Re: {fd}< and compound commands
  2011-09-14 19:06 ` Peter Stephenson
@ 2011-09-14 19:31   ` Stephane Chazelas
  2011-09-14 21:05     ` Peter Stephenson
  2011-09-15 12:58   ` Stephane Chazelas
  2011-09-17 12:21   ` Stephane Chazelas
  2 siblings, 1 reply; 10+ messages in thread
From: Stephane Chazelas @ 2011-09-14 19:31 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

2011-09-14 20:06:01 +0100, Peter Stephenson:
[...]
> {
>   exec {fd}<file
> 
>   while read; do
>     ...
>   done <&$fd
> } always {
>   exec {fd}<&-
> }

Thanks Peter,

Why the "always" here?

> (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.)
[...]

Well, using fd 0 here is dangerous as you never know if commands
in the loop will attempt to read from it (think of ssh without -n
for instance), or you may actually want them to read from (the
original) stdin.

-- 
Stephane


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

* Re: {fd}< and compound commands
  2011-09-14 19:31   ` Stephane Chazelas
@ 2011-09-14 21:05     ` Peter Stephenson
  0 siblings, 0 replies; 10+ messages in thread
From: Peter Stephenson @ 2011-09-14 21:05 UTC (permalink / raw)
  To: zsh-users

Stephane Chazelas wrote:
> 2011-09-14 20:06:01 +0100, Peter Stephenson:
> [...]
> > {
> >   exec {fd}<file
> > 
> >   while read; do
> >     ...
> >   done <&$fd
> > } always {
> >   exec {fd}<&-
> > }
> 
> Thanks Peter,
> 
> Why the "always" here?

That's extended { ... } syntax to ensure the clean-up gets executed even
if there's an error in the main block.  In fact, if I'd known Ruby when
I wrote it, I'd probably have called it "ensure" instead.

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


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

* Re: {fd}< and compound commands
  2011-09-14 19:06 ` Peter Stephenson
  2011-09-14 19:31   ` Stephane Chazelas
@ 2011-09-15 12:58   ` Stephane Chazelas
  2011-09-15 13:09     ` Peter Stephenson
  2011-09-17 12:21   ` Stephane Chazelas
  2 siblings, 1 reply; 10+ messages in thread
From: Stephane Chazelas @ 2011-09-15 12:58 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

2011-09-14 20:06:01 +0100, Peter Stephenson:
> On Wed, 14 Sep 2011 18:21:48 +0100
> Stephane Chazelas <stephane_chazelas@yahoo.fr> 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.
[...]

I just noticed ksh93 doesn't close the fd either, though it
supports compound commands:

$ ksh93 -c '{ echo test >&$fd; } {fd}> a; echo foo >&$fd; nl a'
     1  test
     2  foo

I suppose those were primarily meant to be used with "exec".

Looking at the changelogs for ksh93 and zsh, it seems both
shells implemented it within one week of each other (in April
2005), you guys surely must have mentionned it to each other, or
does that feature come from another shell/language?

KSH> 05-04-08 +Redirection operators can be directly preceded with {varname}
KSH>           with no intervening space, where varname is a variable name which
KSH>           allows the shell to select a file descriptor > 10 and store it
KSH>           into varname.

ZSH> 2005-04-12  Peter Stephenson  <pws@csr.com>
[...]
ZSH>         * 21133: Doc/Zsh/redirect.yo, Src/exec.c, Src/parse.c, Src/text.c,
ZSH>         Src/zsh.h, Test/A04redirect.ztst: New {myfd}> syntax for
ZSH>         allocating file descriptors.

-- 
Stephane


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

* Re: {fd}< and compound commands
  2011-09-15 12:58   ` Stephane Chazelas
@ 2011-09-15 13:09     ` Peter Stephenson
  2011-09-15 13:22       ` Chet Ramey
  0 siblings, 1 reply; 10+ messages in thread
From: Peter Stephenson @ 2011-09-15 13:09 UTC (permalink / raw)
  To: zsh-users

On Thu, 15 Sep 2011 13:58:22 +0100
Stephane Chazelas <stephane_chazelas@yahoo.fr> wrote:
> Looking at the changelogs for ksh93 and zsh, it seems both
> shells implemented it within one week of each other (in April
> 2005), you guys surely must have mentionned it to each other, or
> does that feature come from another shell/language?

Yes, we discussed it.  I think one of us (not me) suggested the syntax.

The fact that there's no automatic closure for the descriptor is what
makes me comfortable with the current limitations --- as I said, the
fact that it looks like a redirection is really a bit of a confusion.

-- 
Peter Stephenson <pws@csr.com>            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
More information can be found at www.csr.com. Follow CSR on Twitter at http://twitter.com/CSR_PLC and read our blog at www.csr.com/blog


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

* Re: {fd}< and compound commands
  2011-09-15 13:09     ` Peter Stephenson
@ 2011-09-15 13:22       ` Chet Ramey
  2011-09-15 14:21         ` Stephane Chazelas
  0 siblings, 1 reply; 10+ messages in thread
From: Chet Ramey @ 2011-09-15 13:22 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users, chet.ramey

On 9/15/11 9:09 AM, Peter Stephenson wrote:

>> does that feature come from another shell/language?
> 
> Yes, we discussed it.  I think one of us (not me) suggested the syntax.

Oliver Kiddle.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, ITS, CWRU    chet@case.edu    http://cnswww.cns.cwru.edu/~chet/


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

* Re: {fd}< and compound commands
  2011-09-15 13:22       ` Chet Ramey
@ 2011-09-15 14:21         ` Stephane Chazelas
  0 siblings, 0 replies; 10+ messages in thread
From: Stephane Chazelas @ 2011-09-15 14:21 UTC (permalink / raw)
  To: Chet Ramey; +Cc: Peter Stephenson, zsh-users

2011-09-15 09:22:35 -0400, Chet Ramey:
> On 9/15/11 9:09 AM, Peter Stephenson wrote:
> 
> >> does that feature come from another shell/language?
> > 
> > Yes, we discussed it.  I think one of us (not me) suggested the syntax.
> 
> Oliver Kiddle.
[...]

Oh, I hadn't realised bash supported it too (since
bash-4.1-alpha according to CHANGES).

For completeness, looks like bash behaves like ksh93 (no close,
works with compound commands).

So looks like if we want to be portable accross all 3 shells,
we'd need to write it:

exec {fd}> file
... >&$fd
exec {fd}>&-

BTW, zsh protects its own internal fds. Looks like bash and
ksh93 don't:

~$ zsh -c 'fd=11; exec {fd}>&-'
zsh:1: file descriptor 11 used by shell, not closed
~$ bash -c 'fd=10; { exec {fd}>&-; } 2> /dev/null; echo test >&2'
~$ ksh93 -c 'fd=10; { exec {fd}>&-; } 2> /dev/null; echo test >&2'
~$

-- 
Stephane


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

* Re: {fd}< and compound commands
  2011-09-14 19:06 ` Peter Stephenson
  2011-09-14 19:31   ` Stephane Chazelas
  2011-09-15 12:58   ` Stephane Chazelas
@ 2011-09-17 12:21   ` Stephane Chazelas
  2 siblings, 0 replies; 10+ messages in thread
From: Stephane Chazelas @ 2011-09-17 12:21 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

2011-09-14 20:06:01 +0100, Peter Stephenson:
[...]
> You need to do:
> 
> local fd
> 
> {
>   exec {fd}<file
> 
>   while read; do
>     ...
>   done <&$fd
> } always {
>   exec {fd}<&-
> }
[...]

Except that if the first exec fails, that might close the wrong
fd.

Maybe:

if command exec {fd}<file; then
  {
    while read <&$fd; do
      ...
    done
  } always {
    exec {fd}<&-
  }
fi

-- 
Stephane


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

end of thread, other threads:[~2011-09-17 12:27 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-09-14 17:21 {fd}< and compound commands Stephane Chazelas
2011-09-14 17:36 ` Mikael Magnusson
2011-09-14 19:06 ` Peter Stephenson
2011-09-14 19:31   ` Stephane Chazelas
2011-09-14 21:05     ` Peter Stephenson
2011-09-15 12:58   ` Stephane Chazelas
2011-09-15 13:09     ` Peter Stephenson
2011-09-15 13:22       ` Chet Ramey
2011-09-15 14:21         ` Stephane Chazelas
2011-09-17 12:21   ` Stephane Chazelas

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