在以下代码中,
#!/bin/bash
sDir=/a/b/c
dDir=/d/e/f
rDir="$dDir/recent"
shopt -s nullglob
:
rm $rDir/$deviceName*
:
问题行rm $rDir/$deviceName*
在没有回显命令的情况下显示丢失的操作数
rm: missing operand
Try 'rm --help' for more information.
如何解决此错误?
发布于 2018-02-23 20:47:06
因为有shopt -s nullglob
,所以当glob模式不匹配时,命令rm $rDir/$deviceName*
扩展为rm
。
实际上,不使用参数调用rm
会产生您看到的消息:
$ rm
rm: missing operand
Try `rm --help' for more information.
对比这两种情况:
$ 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.
一种简单、不安全的方法是将rm
中的错误沉默在缺少的参数上,将其称为rm -f
。
请注意,这样做可能会好得多,这样可以避免使用从未设置变量中生成的参数调用rm
:例如,set -o nounset
将禁止使用未设置变量(但不会对设置为空字符串的变量进行任何操作);如果要对两个变量都未设置或为空,则参数将变为/*
,即根目录中的所有文件。
https://unix.stackexchange.com/questions/426178
复制相似问题