开发者

Too many argument caused by wildcard

I wrote a shell script , and a portion of the script failed, and it said too many arguments:

开发者_如何学编程
if [ -f ABC_DEF_*.* ]; then

What I want to do , is to test whether there are any such file matching the string, but the shell complain that is too many arguments. In the directory there are 20 such files.

Would it be the shell expanded the wildcard and turn ABC_DEF_. into a list of 20 filenames? If yes, how can I resolved this?


The problem is that you can not use [ -f <more then one> ]. It doesn't even make sense: is it returning true when all files exist or when at least one file exist?

if you want to test for existence, do NUM=$(ls <pattern>|wc -l)


You can use [ -f <filename with wildcard> ]. It returns true if one or more files exist that match the filename with the wildcard, with the caveat, if there are more files that match than the if statement can use, it throws the 'too many' error and does not execute the statements in the if block. If the if statement was [ ! -f <filename with wildcard> ], it would work as expected in all cases. Having said that, your solution works for the op's desired behavior (and doesn't reverse the logic when the error occurs).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜