0

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.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Anku g
  • 101
  • 8

1 Answers1

0

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 szM. You need to write it as ${sz}M.

Mario Cianciolo
  • 1,223
  • 10
  • 17