You are not logged in.

#1 2013-01-05 11:24:07

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved] zsh for loop "breaks" differently in terminal vs script

Hi!

The following example shows how my test for loop "breaks" (exits / ends?) a script (when no filenames match), but it continues fine when I paste it into a terminal. I don't understand why this is happening:

% cat ./test;
#!/bin/zsh
for filea in x*y; do echo bla; done; echo test;
% ./test
./test:2: no matches found: x*y
% source ./test
bla
test

Any hints? Thanks!

Last edited by whoops (2013-01-05 13:54:11)

Offline

#2 2013-01-05 12:30:27

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: [solved] zsh for loop "breaks" differently in terminal vs script

One test would be to put an "echo $PWD" or "ls" before the for loop.

Last edited by Trilby (2013-01-05 12:30:54)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2013-01-05 12:57:21

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] zsh for loop "breaks" differently in terminal vs script

Don't understand why, but there, maybe this is easier to read:

% cat test
#!/bin/zsh
echo "####"
echo $PWD
echo "####"
ls
echo "####"
for filea in x*y; do echo inside loop; done; echo after loop;
echo "####"
% source test
####
/tmp/fotest
####
test
####
inside loop
after loop
####
% ./test
####
/tmp/fotest
####
test
####
./test:7: no matches found: x*y

Last edited by whoops (2013-01-05 12:57:47)

Offline

#4 2013-01-05 13:24:50

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,520
Website

Re: [solved] zsh for loop "breaks" differently in terminal vs script

Well that answers one question: it is looking at the same set of files.

I just tried this, and I get the same "No matches found" error message regardless of whether I run the script or source the script.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2013-01-05 13:38:01

knopwob
Member
From: Hannover, Germany
Registered: 2010-01-30
Posts: 239
Website

Re: [solved] zsh for loop "breaks" differently in terminal vs script

There are different config files used for interactive sessions and scripts.
I think for example your .zshrc isn't read when you execute a script.

So the option NOMATCH is set differently when you run a script, or using an interactive shell.

zshoptions wrote:

NOMATCH (-3)
    If a pattern for filename generation has no matches, print an error, instead of leaving it unchanged in the argument list. This also applies to file expansion of an initial ~ or =.

This becoms more clear when you echo the loop variable in interactive mode

/tmp/foo> for var in x*y; do echo $var; done
x*y
/tmp/foo> 

Offline

#6 2013-01-05 13:53:48

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] zsh for loop "breaks" differently in terminal vs script

Thanks, that explains everything!

% cat test
#!/bin/zsh
setopt NULLGLOB
for filea in x*y; do echo inside loop || 0; done || 0; echo after loop; echo "####";
% source test
after loop
####
% ./test
after loop
####

Offline

Board footer

Powered by FluxBB