简体   繁体   English

getopts在Linux Shell脚本中引发无效参数错误

[英]getopts throwing invalid argument error in linux shell script

I am trying to run a shell code with getopts arguments passing at run time. 我正在尝试在运行时传递带有getopts参数的shell代码。 But below script is throwing "Invalid Argument" error. 但是下面的脚本抛出“无效参数”错误。

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

Can someone help me on this why the script is failing? 有人可以帮我解决这个脚本失败的原因吗?

Many Thanks in advance. 提前谢谢了。

getopts only parses single character arguments. getopts仅解析单字符参数。 You need to parse $opt variable like explained here . 你需要分析$opt变量一样解释在这里

If you need long parameters parsing, use the subtly differently named getopt . 如果需要长参数解析,请使用稍有不同的名称getopt

That said, there are a lot of typos in your script: for example size should be siz . 就是说,您的脚本中有很多错别字:例如size应该是siz
Also, be careful when you insert variables inline: $szM will be interpreted as variable szM and not as variable sz M. You need to write it as ${sz}M . 此外,当你插入变量内嵌要小心: $szM将被解释为variable szM而不是variable sz M.你需要写为${sz}M

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

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