hig*_*guy 20 bash wildcards for
在 bash 中,我经常使用 for 循环,如下所示
for file in *.type; do
sommecommand "$file";
done;
Run Code Online (Sandbox Code Playgroud)
对所有匹配的文件执行操作*.type。如果在工作目录中没有找到具有此结尾的文件,则星号不会展开,通常我会收到一条错误消息,指出 somecommand 没有找到该文件。我可以立即想到几种方法来避免这个错误。但是添加一个条件似乎不是很优雅。有没有一种简短而干净的方法来实现这一目标?
Gil*_*not 23
是的,运行以下命令:
shopt -s nullglob
Run Code Online (Sandbox Code Playgroud)
它将使匹配无效并且不会触发任何错误。
~/.bashrc如果您想在 POSIX shell 中检测空 glob,请尝试
for i in *.txt; do
[ "$i" = '*.txt' ] && [ ! -e '*.txt' ] && continue
done
Run Code Online (Sandbox Code Playgroud)见http://mywiki.wooledge.org/NullGlob
对于没有选项的 shell,通常的技术nullglob是
set -- [*].type *.type
case $1$2 in
'[*].type*.type') shift 2;;
*) shift
esac
for file do
cmd -- "$file"
done
Run Code Online (Sandbox Code Playgroud)
额外的内容是为了涵盖当前目录中[*].type调用一个文件的情况。*.type
现在,如果您想包含点文件,那就变得更加复杂。
我相信这项技术是几年前由 Laura Fairhead 在 usenet 上创造的。
在 bash 中,shopt -s nullglob如果没有匹配项,您可以使用扩展为空数组。
在没有 的 POSIX shell 中nullglob,您可以通过将传递的文件名[ -e "$file" ] || [ -L "$file" ] || continue作为for循环的第一部分来检查传递的文件名是否实际存在来避免此问题。
| 归档时间: |
|
| 查看次数: |
3940 次 |
| 最近记录: |