gpt4 book ai didi

linux - getopts 在 linux shell 脚本中抛出无效参数错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:11:36 24 4
gpt4 key购买 nike

我正在尝试使用在运行时传递的 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

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

非常感谢。

最佳答案

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

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

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

关于linux - getopts 在 linux shell 脚本中抛出无效参数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48705536/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com