简体   繁体   中英

getopts throwing invalid argument error in linux shell script

I am trying to run a shell code with getopts arguments passing at run time. 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. You need to parse $opt variable like explained here .

If you need long parameters parsing, use the subtly differently named getopt .

That said, there are a lot of typos in your script: for example size should be 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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