You are not logged in.
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
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
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
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
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.
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
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