To add options, I have the following in one of my scripts:
parse_opts() {
while [ $# -gt 0 ]; do
case "$1" in
-h|--help)
help=1
shift
;;
-r|--raw)
raw=1
shift
;;
-c|--copy)
copy=1
shift
;;
-s|--sensitive)
sensitive=1
shift
;;
-i|--insensitive)
insensitive=1
shift
;;
*)
shift
;;
esac
done
}
There is only one problem, I cannot use multiple options at the same time. For example, using -r -s works but using -rs does not. How can I programmatically make it work without adding separate entries? The shell I am using is sh.
getopts?getoptsfrom ksh93 does long options though (and not even the same was as GNUgetopt_long()does).util-linuxgetoptcan do it though.getoptsmakes it vastly more capable than your own implementation. For example, your code doesn't support combining options, like-rc, nor does your code complain given invalid options, nor does it support arguments to options. Follow that link for a better solution implementing long and short options with optional arguments and proper errors.