I am writing a bash script that uses calling syntax
script [options] [dir]
To retrieve the set of options and parse them I use getopts. But how can I get the dir argument? In general, if I retrieve the last argument as ${@:${#@}}, it does not have to be the dir, it can be still an option or a value of it.
Code I use for getopts:
DIR="."
RECURSIVE=
FILTER=
while getopts “hnf:” OPTION
do
case $OPTION in
h)
usage
exit 1
;;
n)
RECURSIVE="-maxdepth 1"
;;
f)
FILTER=$OPTARG
;;
\?)
exit 1
;;
:)
exit 1
;;
esac
done
Can you help?
OPTINDstores the position of the processed parameter. After the loop do a:Now the directories are in
$@, first directory is in$1.