bash - 如果没有扩展名为 .c 的文件,则 for 循环包括通配符 (*.c)。我怎么走?

标签 bash shell unix scripting for-loop

<分区>

我在 UNIX (sparc 10) 上使用 bash

for file in $SCPATH/$LIBNAME/*.{gob,c,cpp,h};
do
    ln -s $file;
done;

问题是:如果没有扩展名为 'c' 的文件,它会将 ".c"放入 $file 中,ln -s 将创建指向 '.c' 的链接。 这是一个已知问题吗?我怎样才能绕过它(除了明显的“if not *.c”黑客攻击)。

最佳答案

你需要在循环之前设置 nullglob

shopt -s nullglob

nullglob: If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves.

当您完成并想要将其重置为原始行为时,请使用:

shopt -u nullglob

正如 Dennis Williamson 在其中一条评论中指出的那样。

关于bash - 如果没有扩展名为 .c 的文件,则 for 循环包括通配符 (*.c)。我怎么走?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5123135/

相关文章:

linux - 如何在可视模式下用 HTML 标签重复 VIM 环绕

linux - 如何根据第一个脚本的结果从另一个 shell 脚本调用 shell 脚本?

linux - 我的 sed 命令是否正确解析来自 JSON 响应的特定键的值

linux - linux中移动文件的自定义命令

PHP shell_exec 不在后台运行

c - 在简单的 shell 中实现历史函数(C 语言)

bash - Shell 脚本模板

bash - sudo cat << EOF > 文件不起作用,sudo su 可以

bash - 带参数的 MySQLdump

linux - 在远程运行 sudo 脚本/bash 命令