如果没有扩展名为.c的文件,则for循环包括通配符(* .c).我该怎么走? [英] for loop includes wild characters (*.c) if no files with extension .c. How do I get around?
问题描述
我在UNIX(sparc 10)上使用bash
I am using bash on UNIX (sparc 10)
for file in $SCPATH/$LIBNAME/*.{gob,c,cpp,h};
do
ln -s $file;
done;
问题是:如果没有扩展名为'c'的文件,它将在文件$ file中放入" .c",而ln -s将创建指向' .c'的链接. 这是一个已知问题吗?我该如何解决(除了明显的如果不是* .c"黑客).
The problem is: if there are no files with extension 'c', it will put ".c" in $file and ln -s will create a link to '.c'. Is this a know issue? How can I get around it (besides the obvious 'if not *.c' hack).
推荐答案
您需要在循环之前设置nullglob
you need to set nullglob before your loop
shopt -s nullglob
nullglob: 如果设置,bash允许不匹配文件的模式 (请参见上面的路径名扩展) 扩展为空字符串,而不是 自己.
nullglob: If set, bash allows patterns which match no files (see Pathname Expansion above) to expand to a null string, rather than themselves.
完成后想将其重置为原始行为,请使用:
When you're done and want to reset it to the original behavior, use:
shopt -u nullglob
如丹尼斯·威廉姆森(Dennis Williamson)在评论之一中指出的那样.
As pointed out by Dennis Williamson in one of the comments.
这篇关于如果没有扩展名为.c的文件,则for循环包括通配符(* .c).我该怎么走?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!