for f in *.test
do
echo 'This is f: '${f}
done
之后(chmod x for_loop)我可以用./for_loop启动它.
如果有.test文件,那么everthing就可以了,但是如果文件夹中没有与..test匹配的文件,for循环仍然执行一次.在这种情况下,输出看起来像:
This is f: *.test
但我认为如果文件夹中没有与glob模式匹配的文件,则没有输出.为什么不是这样?
解决方法:
这是默认行为.
要摆脱它,启用nullglob:
shopt -s nullglob
nullglob expands non-matching globs to zero arguments, rather than to
themselves.
…
Without nullglob, the glob would expand to a literal * in an empty
directory, resulting in an erroneous count of 1.
或者使用zsh,默认情况下启用了这个glob!
$for f in *.test; do echo "$f"; done
zsh: no matches found: *.test
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。