zsh-users
 help / color / mirror / code / Atom feed
* Re: assigning parameters and globbing
@ 2006-04-06 12:16 Vincent Lefevre
  0 siblings, 0 replies; 8+ messages in thread
From: Vincent Lefevre @ 2006-04-06 12:16 UTC (permalink / raw)
  To: Zsh users list

On 2006-04-06 10:27:09 +0100, Peter Stephenson wrote:
> Vincent Lefevre wrote:
> > I wonder why there isn't something similar in standard to avoid the "="
> > in assignments, e.g. a typeset option -e: export -e BLAH *([-1])
> 
> This one's easy to do with a function:
> 
>   setexport() { export $1=$2; }
>   setexport BLAH *([-1])

This is what I said in my message, but an option for typeset would
have the advantage to be valid for local and so on, and would allow
other options.

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


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

* Re: assigning parameters and globbing
@ 2006-04-06  9:27 Peter Stephenson
  0 siblings, 0 replies; 8+ messages in thread
From: Peter Stephenson @ 2006-04-06  9:27 UTC (permalink / raw)
  To: Zsh users list

Vincent Lefevre wrote:
> I wonder why there isn't something similar in standard to avoid the "="
> in assignments, e.g. a typeset option -e: export -e BLAH *([-1])

This one's easy to do with a function:

  setexport() { export $1=$2; }
  setexport BLAH *([-1])

It's harder if you're trying to do something that makes BLAH local to
the immediately enclosing scope---zsh lets you set variables globally or
in the current scope but not in the immediately surrounding scope---but
you don't want that in this case.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: assigning parameters and globbing
@ 2006-04-05 23:18 Vincent Lefevre
  0 siblings, 0 replies; 8+ messages in thread
From: Vincent Lefevre @ 2006-04-05 23:18 UTC (permalink / raw)
  To: Zsh users list

On 2006-04-05 08:00:56 -0700, Bart Schaefer wrote:
>     files=(*) eval 'export BLAH=$files[-1]'

This would perhaps be more efficient:

  files=(*([-1])) eval 'export BLAH=$files'

Or perhaps I could reuse my old setenv function:

setenv() { export "$1=$2" }
setenv BLAH *([-1])

I wonder why there isn't something similar in standard to avoid the "="
in assignments, e.g. a typeset option -e: export -e BLAH *([-1])

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


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

* Re: assigning parameters and globbing
@ 2006-04-05 15:00 Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2006-04-05 15:00 UTC (permalink / raw)
  To: Zsh users list

On Apr 5, 10:24am, Peter Stephenson wrote:
}
} I'd probably use an intermediate array.
} 
}   local -a files
}   files=(*)
}   export BLAH=${files[-1]}

Also, arrays can be placed into the environment of builtin commands,
even though they can't be exported to the "permanent" environment that
is shared by external commands.  So this works:

    files=(*) eval 'export BLAH=$files[-1]'


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

* Re: assigning parameters and globbing
@ 2006-04-05  9:24 Peter Stephenson
  0 siblings, 0 replies; 8+ messages in thread
From: Peter Stephenson @ 2006-04-05  9:24 UTC (permalink / raw)
  To: Zsh users list

Vincent Lefevre wrote:
> What do you suggest to set an environment BLAH to *([-1]) or something
> similar (globbing that leads to a single filename -- which may contain
> special characters)?

You can, if you like, use glob_assign:

  setopt GLOB_ASSIGN
  export BLAH
  BLAH=*([-1])

but I'd probably use an intermediate array.

  local -a files
  files=(*)
  export BLAH=${files[-1]}

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: assigning parameters and globbing
@ 2006-04-04 22:34 Vincent Lefevre
  0 siblings, 0 replies; 8+ messages in thread
From: Vincent Lefevre @ 2006-04-04 22:34 UTC (permalink / raw)
  To: Zsh users list

On 2006-04-04 13:58:17 +0100, Peter Stephenson wrote:
> Note that the syntax you're using relies on array handling even though
> you're explicitly extracting a single element at the end, and even
> though it's smart enough to make BLAH a scalar in the case of a normal
> assignment.  Indeed, GLOB_ASSIGN is vaguely deprecated (meaning there
> are no plans to remove it but it's safer not to use it) partly because
> you can't predict if the result of the assignment is a scalar or an
> array.

What do you suggest to set an environment BLAH to *([-1]) or something
similar (globbing that leads to a single filename -- which may contain
special characters)?

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


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

* Re: assigning parameters and globbing
@ 2006-04-04 12:58 Peter Stephenson
  0 siblings, 0 replies; 8+ messages in thread
From: Peter Stephenson @ 2006-04-04 12:58 UTC (permalink / raw)
  To: Zsh users list

Vincent Lefevre wrote:
> Is it normal that this doesn't work?
> 
> dixsept% setopt GLOB_ASSIGN
> dixsept% export BLAH=*([-1])
> zsh: no matches found: BLAH=*([-1])

Yes, it is.  Assignments inside builtins have always been a bit fraught
since the arguments are parsed as normal arguments, which is a rather
different mechanism from the way stand-alone assignments are handled.

There are a few exceptions, for example ~'s are handled immediately
after the = and after colons for PATH-like variables, and word-splitting
isn't done in cases like

  export foo=$(echo two words)

which would strictly expand to

  export foo=two words

which probably isn't what you meant.

However, there's never been any attempt to handle arrays in that
position, so it's not only GLOB_ASSIGN that doesn't work; no form of
array assignment works after typeset and friends.

Note that the syntax you're using relies on array handling even though
you're explicitly extracting a single element at the end, and even
though it's smart enough to make BLAH a scalar in the case of a normal
assignment.  Indeed, GLOB_ASSIGN is vaguely deprecated (meaning there
are no plans to remove it but it's safer not to use it) partly because
you can't predict if the result of the assignment is a scalar or an
array.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* assigning parameters and globbing
@ 2006-04-04 12:01 Vincent Lefevre
  0 siblings, 0 replies; 8+ messages in thread
From: Vincent Lefevre @ 2006-04-04 12:01 UTC (permalink / raw)
  To: zsh-users

Is it normal that this doesn't work?

dixsept% BLAH=*([-1])
dixsept% echo $BLAH
*([-1])
dixsept% export BLAH=*([-1])
zsh: no matches found: BLAH=*([-1])

dixsept% setopt GLOB_ASSIGN
dixsept% BLAH=*([-1])
dixsept% echo $BLAH        
xc
dixsept% export BLAH=*([-1])
zsh: no matches found: BLAH=*([-1])

I have zsh 4.3.2-dev-1 (Debian/unstable).

-- 
Vincent Lefèvre <vincent@vinc17.org> - Web: <http://www.vinc17.org/>
100% accessible validated (X)HTML - Blog: <http://www.vinc17.org/blog/>
Work: CR INRIA - computer arithmetic / SPACES project at LORIA


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

end of thread, other threads:[~2006-04-06 12:16 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-04-06 12:16 assigning parameters and globbing Vincent Lefevre
  -- strict thread matches above, loose matches on Subject: below --
2006-04-06  9:27 Peter Stephenson
2006-04-05 23:18 Vincent Lefevre
2006-04-05 15:00 Bart Schaefer
2006-04-05  9:24 Peter Stephenson
2006-04-04 22:34 Vincent Lefevre
2006-04-04 12:58 Peter Stephenson
2006-04-04 12:01 Vincent Lefevre

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