while getopts a:b:c: flag
do
case "${flag}" in
a) username=${OPTARG};;
b) password=${OPTARG};;
c) var1=${OPTARG};;
esac
done
echo "Checking"
if [[ 'echo "$OPTARG" | grep -Eq ^\\-.+$' != 0 ]];
then
echo "$0: option requires an argument -- ${OPT}"
exit 1
fi
In case I am using one of the flags without any argument ./test.sh -a testarg -c
I am getting the output like that:
./test.sh: option requires an argument -- c
Checking
./test.sh: option requires an argument --
As you can see the output is quite strange instead of just one error message. I have added a Checking
message to show that it appears in between. How to check in the correct way if the flag has been entered without any argument and stop the script
or at least in case of using flag -c
without any argument to make variable var1=true
but looks like bash can't support that.