I'm trying to get the following as valid calls of my script:
sh name.sh -l -a
sh name.sh -l
This is the code I have so far using getopts, where -a is an required argument:
default="no"
echo "Initial parameters.
while getopts ":l:a:" o; do
case "${o}" in
l)
...;;
a)
a+=(${OPTARG})
IFS=',' read -a myarray <<< "$a"
default="yes"
;;
:)
echo "Missing option argument for -$OPTARG" >&2; exit 1;;
*)
usage;;
esac
done
shift $((OPTIND-1))
if [ -z "${l}" ] || [ -z "${a}" ] ; then
usage
fi
I just need to know how to set in getopts the optional flag -a with it's argument. Thanks:)