while getopts "hd:R:" arg; do
case $arg in
h)
echo "usage"
;;
d)
dir=$OPTARG
;;
R)
if [[ $OPTARG =~ ^[0-9]+$ ]];then
level=$OPTARG
else
level=1
fi
;;
\?)
echo "WRONG" >&2
;;
esac
done
-
level refers to the parameter of
-R, dir refers to parameters of-d -
when I input
./count.sh -R 1 -d test/it works correctly -
when I input
./count.sh -d test/ -R 1it works correctly -
but I want to have it work when I input
./count.sh -d test/ -Ror./count.sh -R -d test/
This means that I want -R to have a default value and for the sequence of commands to be more flexible.
getoptsdoesn’t really support this; but it’s not hard to write your own replacement.