Hi,
while using getopt for reading the commandline args for my script I am expecting it to be reading allowed values or else exit with error.
pls find the code getopt.sh
Code:
#!/bin/bash
# getopt.sh example
echo "BEFORE GETOPT: $@";
# Execute getopt
ARGS=$(getopt -o a:b:c -l "ay:,bee:cee" -n "getopt.sh" -- "$@");
#Bad arguments
if [ $? -ne 0 ];
then
echo "ERROR ...."
exit 1
fi
echo "BEFORE GETOPT: $@";
eval set -- "$ARGS";
So here I am expecting my script will accept args
-a, -b, -c , --ay ,--bee , --cee only and if not it should fail with ERROR.
But it is not failing in that case. How to make my script fail incase of wrong value of args?
OUTPUT:
1: SUCCESS case
# ./getopt.sh -a value
BEFORE GETOPT: -a value
BEFORE GETOPT: -a value
2:FAILURE CASE(SHOULD FAIL!!!)
# ./getopt.sh -am value
BEFORE GETOPT: -am value
BEFORE GETOPT: -am value
3:FAILURE CASE(SHOULD FAIL!!!)
# ./getopt.sh -be value
BEFORE GETOPT: -be value
BEFORE GETOPT: -be value
Thanks in advance
Rasna