(Similar to this , but in bash.)
I have an existing bash script that uses built-in getopts for recognition -s(only a flag - no argument). I find that I use this parameter every time, so I want to make it the default if -s-or is not specified on the command line +s. However, although ksh getoptscan handle it+s , I cannot find this feature in the bash getoptsmanual .
My current workarounds:
- use
s:so that I can recognize -s-on $OPTARG= "-"; or - replace
-swith another option, for example -d(for "do not").
However, in # 1 there is a problem that it swallows the next argument if I accidentally indicates -s, and # 2 has a problem with the fact that it uses a different shift letter than the one I already have in my muscle memory. I hope there may be an easy way to parse -s-or +sin bash.
- util-linux getopt (1) also does not
+s. It can handle optional arguments, so it can accept -s-. However, I have not used it getopt(1)before, so I would like pointers on how not to shoot in the leg. - BashFAQ 035 says, "figure it out for yourself." if you have already written a program that does
+sor -s-, I would really like to see it.
My current arg parsing loop is very simple:
while getopts "nthps" opt ; do
case "$opt" in
<other cases cut>
(s) saw_s="yes"
;;
esac
done
shift $((OPTIND-1))