开发者

for loop includes wild characters (*.c) if no files with extension .c. How do I get around? [duplicate]

This question already has answers here: How to skip the for loop when there are no matching files? (2 answers) Closed 3 years ago.

I am using bash on UNIX (sparc 开发者_JAVA百科10)

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

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).


you need to set nullglob before your loop

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.

When you're done and want to reset it to the original behavior, use:

shopt -u nullglob

As pointed out by Dennis Williamson in one of the comments.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜