简体   繁体   English

使用命令和glob“ echo-?”的shell会有什么行为,我应该期待什么?

[英]What behavior can and should I expect of a shell with the command and glob “echo -?”?

If I want to match a file called "-f" or "-r" I might do something like 如果我想匹配一个名为“ -f”或“ -r”的文件,我可能会做类似的事情

test.sh -?

And if I want to send the literal '-?' 如果我要发送原义的“-?” to a program as an argument I might do something like: 程序作为参数,我可能会做类似的事情:

test.sh -\?

If no such file "-f" or "-r" or anything like it exists, then what should my shell do with 如果不存在这样的文件“ -f”或“ -r”或类似的文件,那么我的shell应该如何处理

test.sh -?

Should it tell me that no file matches this pattern? 它应该告诉我没有文件与此模式匹配吗?

In bash , the default is to treat an unmatched pattern literally. bash ,默认值是按字面意义对待不匹配的模式。 If the nullglob option is set, an unmatched pattern "evaporates"; 如果设置了nullglob选项, nullglob匹配的模式“消失”; it is removed from command, not even expanding to the empty string. 它已从命令中删除,甚至没有扩展为空字符串。

In zsh , an unmatched pattern produces an error by default. zsh ,默认情况下,不匹配的模式会产生错误。 Setting the nomatch option causes an unmatched pattern to be treated literally, and zsh also supports a nullglob option which causes unmatched patterns to disappear. 设置nomatch选项将导致按字面意义对待不匹配的模式,而zsh还支持nullglob选项,该选项将导致不匹配的模式消失。 There is also a cshnullglob option which acts like nullglob , but requires at least one pattern in a command to match, or an error is produced. 还有一个cshnullglob选项,其功能类似于nullglob ,但命令中至少需要一个模式才能匹配,否则会产生错误。

Note that POSIX specifies that if the pattern contains an invalid bracket expression or does not match any existing filenames or pathnames, the pattern string shall be left unchanged in sh . 注意, POSIX指定如果模式包含无效的括号表达式或与任何现有的文件名或路径名都不匹配,则模式字符串应在sh中保持不变。

ash, dash, ksh, bash and zsh all behave this way when invoked as sh. ash,dash,ksh,bash和zsh在作为sh调用时都以这种方式运行。

POSIX specifies that if the pattern contains an invalid bracket expression or does not match any existing filenames or pathnames, the pattern string shall be left unchanged in sh . POSIX指定如果模式包含无效的括号表达式或与任何现有的文件名或路径名都不匹配,则模式字符串应sh 保持不变

ash , dash , ksh , bash and zsh all behave this way when invoked as sh . ashdashkshbashzsh在作为sh调用时都以这种方式运行。

You seem to be looking for the nullglob option, at least with Bash: 您似乎在寻找nullglob选项,至少在Bash中:

shopt -s nullglob

Without the nullglob option, an unmatched pattern is passed as its literal self to your program: the shell will pass -? 如果没有nullglob选项,则将不匹配的模式作为其字面量本身传递给您的程序:shell将传递-? to the script if there isn't a file that matches. 如果没有匹配的文件,请输入脚本。 With the nullglob option, unmatched patterns are replaced with nothing at all. 使用nullglob选项,完全不替换任何不匹配的模式。

If no such pattern exists, the shell, by default, just returns the pattern your gave, including whatever * or ? 如果不存在这样的模式,则默认情况下,shell仅返回您提供的模式,包括*? characters you used. 您使用的字符。 To determine whether the file actually exists, test it. 要确定文件是否实际存在,请对其进行测试。 Thus, inside your script, use: 因此,在脚本中,使用:

[ -f "$1" ] ||  echo "no such file exists"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM