Using `select` command to print a menu in Bash
Your menu shown by select will look like:
1) apache
2) named
3) sendmail
Select an option and press Enter:
At this time, you enter "1" or "2" or "3": you don't type the word.
Also, select
will loop until it sees a break
command, so you want this:
case $opt in
"apache")
date
break
;;
"named")
echo "test"
break
;;
"sendmail")
echo "test 2"
break
;;
*) echo "invalid option";;
esac
If you wanted to allow the user to enter either the number or the word, you could write this:
select opt in "${options[@]}"; do
case "$opt,$REPLY" in
apache,*|*,apache) do_something; break ;;
named,*|*,named) do_something; break ;;
sendmail,*|*,sendmail) do_something; break ;;
esac
done
The comma has no syntactical significance, it's just there to be able to pattern match on either the $REPLY variable (which is what the user actually typed) or the $opt variable
Your $opt
variable will be set to the option word corresponding to the number that the user inputs. If you want to look at what the user actually typed, look at $REPLY
.