I am trying to parse a -temp
option with Bash getopts. I'm calling my script like this:
./myscript -temp /foo/bar/someFile
Here is the code I'm using to parse the options.
while getopts "temp:shots:o:" option; do
case $option in
temp) TMPDIR="$OPTARG" ;;
shots) NUMSHOTS="$OPTARG" ;;
o) OUTFILE="$OPTARG" ;;
*) usage ;;
esac
done
shift $(($OPTIND - 1))
[ $# -lt 1 ] && usage
getopts
can only parse short options.
Most systems also have an external getopt
command, but getopt is not standard, and is generally broken by design as it can't handle all arguments safely (arguments with whitespace and empty arguments), only GNU getopt can handle them safely, but only if you use it in a GNU-specific way.
The easier choice is to use neither, just iterate the script's arguments with a while-loop and do the parsing yourself.
See http://mywiki.wooledge.org/BashFAQ/035 for an example.