在下面的代码中,
#!/bin/bash
sDir=/a/b/c
dDir=/d/e/f
rDir="$dDir/recent"
shopt -s nullglob
:
rm $rDir/$deviceName*
:
Run Code Online (Sandbox Code Playgroud)
问题行 rm $rDir/$deviceName*显示缺少回显命令的操作数
rm: missing operand
Try 'rm --help' for more information.
Run Code Online (Sandbox Code Playgroud)
如何解决此错误?
因为您有shopt -s nullglob,该命令rm $rDir/$deviceName*扩展到仅rm当 glob 模式不匹配时。
实际上,rm不带参数调用会导致您看到以下消息:
$ rm
rm: missing operand
Try `rm --help' for more information.
Run Code Online (Sandbox Code Playgroud)
对比这两个:
$ rm nonexistent*
rm: cannot remove `nonexistent*': No such file or directory
$ (shopt -s nullglob; rm nonexistent*)
rm: missing operand
Try `rm --help' for more information.
Run Code Online (Sandbox Code Playgroud)
一种简单的、不安全的、消除rm丢失参数错误的方法是将其称为rm -f.
请注意,它可能会更好,相反,避免以rm首先set -o nounset使用未设置变量构成的参数调用的情况结束:将禁止使用未设置变量(但不会对设置为空字符串的变量执行任何操作), 例如; 如果您将rm $x/$y*两个变量都设置为未设置或为空,则参数将变为/*,这意味着所有文件都直接位于根目录中。