I am trying to implement getopts into my Bash script for passing command line arguments, yet I am encountering difficulty with the actual syntax and long options support. The man for getopts did not include clear examples, so most examples came from other scripts on the net, which may not have the correct syntax. In addition, several approaches exist leading to more confusion with a proper explanation. Please let know if long options are a possibility with getopts. Long options are options flags like --files or --verbose while shot options are -f or -v.
Code:
GETOPT=$(getopts -o fdp:uvh -n ${SCRIPTNAME} -- "$@")
if [ $? -ne 0 ]; then
usage
fi
eval set -- "$GETOPT"
while [ "$1" != "" ]; do
case "$1" in
-f | --files ) FLAG_FILES="true"
;;
-d | --directories ) FLAG_DIR="true"
;;
-p | --preset ) shift
PRESET=$1
;;
-u | --undo ) FLAG_UNDO="true"
;;
-v | --verbose ) VERBOSE="yes"
;;
-h | --help ) usage
exit
;;
* ) usage
exit 1
esac
shift
done
Bash return a /home/neville/bin/Filerenamer.sh: line 103: getopts: -o: invalid option. Please lead me to a syntax that support long options.