I want getopt to send my user to a help page if they don't supply an option. Goal: If the user specifies no command line or -h
, set flag_help
to 1.
flag_help=0
flag_test_user=0
flag_demo_user=1
flag_skip_initial_pause=0
while getopts 'hdts' flag; do
case ${flag} in
h)
flag_help=1
;;
d)
echo "Using Demo Mode"
flag_test_user=0
flag_demo_user=1
;;
t)
echo "Using Test Mode"
flag_test_user=1
flag_demo_user=0
;;
s)
echo "Will skip initial pause.."
flag_skip_initial_pause=1
;;
*)
flag_help=1
;;
esac
done
echo "flag_help: $flag_help"
I then test it:
$ sh tools/setup-demo.sh
flag_help: 0
$ sh tools/setup-demo.sh -h
flag_help: 1
I must have some oddball error in there, but I'm not sure what. To be safe, I did some research and there is a great example thread here: An example of how to use getopts in bash
My syntax seems correct based on that.
What am I doing wrong?