【问题标题】:getopts throwing invalid argument error in linux shell scriptgetopts 在 linux shell 脚本中抛出无效参数错误
【发布时间】:2018-02-09 12:09:49
【问题描述】:

我正在尝试运行带有在运行时传递的 getopts 参数的 shell 代码。但下面的脚本抛出“无效参数”错误。

strt_tim=`date`
while getopts dir:day:size: arg; do
case "$arg" in
dir) dirnm="$OPTARG";;
day) dy="$OPTARG";;
siz) sz="$OPTARG";;
*) echo "Invalid arg";;
esac
done
echo
find $dirnm -mtime -$dy -size +$szM -exec ls -lh \
{} \; | awk '{print $3, $4, $5, $6, $7, $8, $9}'

Executing shell script:

sh delutil.sh -dir /path/of/dir/ -day 10 -siz 100

有人可以帮我解决脚本失败的原因吗?

非常感谢。

【问题讨论】:

    标签: linux getopts


    【解决方案1】:

    getopts 仅解析单个字符参数。您需要解析$opt 变量,如here 解释的那样。

    如果您需要解析长参数,请使用名称略有不同的getopt

    也就是说,您的脚本中有很多拼写错误:例如,size 应该是 siz
    此外,在插入内联变量时要小心:$szM 将被解释为variable szM 而不是variable szM。你需要写成${sz}M

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-28
      • 2019-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      相关资源
      最近更新 更多