zsh-users
 help / color / mirror / code / Atom feed
* globbing in a for loop may prematurely exit script when sourcing a zsh script
@ 2013-07-31  8:13 Chris Hiestand
  2013-07-31  9:02 ` Peter Stephenson
  0 siblings, 1 reply; 3+ messages in thread
From: Chris Hiestand @ 2013-07-31  8:13 UTC (permalink / raw)
  To: zsh-users


I am new to zsh and this is what I'm trying to do within /etc/zsh/zshenv:

> if [[ -d "/custom/zsh.d" ]]; then
>   for f in /custom/zsh.d/*.zsh; do
>     source $f
>   done 2> /dev/null
> fi
> 
> #Do more stuff below...

What happens in zsh 4.3.17 is that if /custom/zsh.d/*.zsh expands to no files, zsh acts as if it
hits err_return and "Do more stuff" does not get executed.


Here is a test script test.zsh:

> #!/usr/bin/zsh
> setopt
> unsetopt histignorealldups
> unsetopt login
> unsetopt monitor
> unsetopt sharehistory
> unsetopt zle
> echo 'setopt again'
> setopt
> 
> 
> echo "Test 1"
> 
> ls /tmp/doesnotexist*
> 
> echo "Test 1 passed"
> 
> echo "Test 2"
> 
> if [ -d "/usr" ]; then
>   for f in /usr/*.zsh; do
>     echo $f
>   done
> fi
> 
> echo "Test 2 passed"



> user@host /tmp % ./test.zsh                                                                                                                             
> nohashdirs
> setopt again
> nohashdirs
> Test 1
> ./test.zsh:15: no matches found: /tmp/doesnotexist*
> Test 1 passed
> Test 2
> ./test.zsh:22: no matches found: /usr/*.zsh
> Test 2 passed
> 


> user@host /tmp % source test.zsh                                                                                                                       
> interactive
> setopt again
> interactive
> Test 1
> test.zsh:15: no matches found: /tmp/doesnotexist*
> Test 1 passed
> Test 2
> test.zsh:22: no matches found: /usr/*.zsh
> # Test 2 never finishes!


It seems that this problem is limited to interactive shells. Is this a bug or is this desirable behavior?

Thanks!

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

* Re: globbing in a for loop may prematurely exit script when sourcing a zsh script
  2013-07-31  8:13 globbing in a for loop may prematurely exit script when sourcing a zsh script Chris Hiestand
@ 2013-07-31  9:02 ` Peter Stephenson
  2013-07-31 19:42   ` Chris Hiestand
  0 siblings, 1 reply; 3+ messages in thread
From: Peter Stephenson @ 2013-07-31  9:02 UTC (permalink / raw)
  To: zsh-users

On Wed, 31 Jul 2013 01:13:47 -0700
Chris Hiestand <chiestand@salk.edu> wrote:
> I am new to zsh and this is what I'm trying to do within /etc/zsh/zshenv:
> 
> > if [[ -d "/custom/zsh.d" ]]; then
> >   for f in /custom/zsh.d/*.zsh; do
> >     source $f
> >   done 2> /dev/null
> > fi
> > 
> > #Do more stuff below...
> 
> What happens in zsh 4.3.17 is that if /custom/zsh.d/*.zsh expands to
> no files, zsh acts as if it hits err_return and "Do more stuff" does
> not get executed.

Historically, the shell has certainly been quite aggressive at treating
glob failures as hard errors.  There are various options you can set to
select how the shell treats glob failures.

The right thing to do here is tell the shell to expand the pattern to an
empty string if there are no matches.  You can do this without changing
any options by appending (N) to the pattern, i.e.

  for f in /custom/zsh.d/*.zsh(N); do

This behaves as if the NULL_GLOB option was set for that pattern.  In
zsh (but not necessarily other shells) a "for" loop over an empty
expansion does nothing.

pws


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

* Re: globbing in a for loop may prematurely exit script when sourcing a zsh script
  2013-07-31  9:02 ` Peter Stephenson
@ 2013-07-31 19:42   ` Chris Hiestand
  0 siblings, 0 replies; 3+ messages in thread
From: Chris Hiestand @ 2013-07-31 19:42 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users


On Jul 31, 2013, at 2:02 AM, Peter Stephenson <p.stephenson@samsung.com> wrote:

> The right thing to do here is tell the shell to expand the pattern to an
> empty string if there are no matches.  You can do this without changing
> any options by appending (N) to the pattern, i.e.
> 
>  for f in /custom/zsh.d/*.zsh(N); do
> 
> This behaves as if the NULL_GLOB option was set for that pattern.  In
> zsh (but not necessarily other shells) a "for" loop over an empty
> expansion does nothing.


Excellent, this is exactly what I was looking for! Thank you, I knew there must
be some way to do it.

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

end of thread, other threads:[~2013-07-31 19:42 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-07-31  8:13 globbing in a for loop may prematurely exit script when sourcing a zsh script Chris Hiestand
2013-07-31  9:02 ` Peter Stephenson
2013-07-31 19:42   ` Chris Hiestand

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