Reputation: 515
I'm new to bash. I want to have a select menu in bash. It has four options. Here is the code:
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Option 4")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice $REPLY which is $opt"
;;
"Option 2")
echo "you chose choice $REPLY which is $opt"
;;
"Option 3")
echo "you chose choice $REPLY which is $opt"
;;
"Exit")
break
;;
*) echo "invalid option $REPLY";;
esac
done
When I execute this code, the first time full menu with 4 options appears, but later times (2nd, 3rd and ... times) only "Please enter your choice:" appears. I want each time all 4 options appear like the first time.
As another problem, I want each time that the program is executed and 4 options are re-appeared, the previous commands and terminal content to be cleared. I did this by putting a "clear" before:
select opt in "${options[@]}"
but this way only the first time the terminal content was cleared.
What can I do to solve these problems?
Upvotes: 1
Views: 1459
Reputation: 4743
You would have to add a new loop that embeds your menu and then add another break
to each case, and for option 4 edit to break 2
as shown in code below. I also added the option to clear the screen after an option is selected.
#!/bin/bash
PS3='Please enter your choice: '
while true; do
clear
options=("Option 1" "Option 2" "Option 3" "Exit")
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice $REPLY which is $opt"
break
;;
"Option 2")
echo "you chose choice $REPLY which is $opt"
break
;;
"Option 3")
echo "you chose choice $REPLY which is $opt"
break
;;
"Exit")
break 2
;;
*) echo "invalid option $REPLY";;
esac
done
done
So a case of use of this code would be:
$ ./select.sh
1) Option 1
2) Option 2
3) Option 3
4) Exit
Please enter your choice: 1
you chose choice 1 which is Option 1
1) Option 1
2) Option 2
3) Option 3
4) Exit
Please enter your choice: 2
you chose choice 2 which is Option 2
1) Option 1
2) Option 2
3) Option 3
4) Exit
Please enter your choice: 3
you chose choice 3 which is Option 3
1) Option 1
2) Option 2
3) Option 3
4) Exit
Please enter your choice: 4
$
In case you want to ensure that the user can read the option selected you can add the following line before the last done
of the code:
read -p "Press [Enter] key to continue..."
That way the user will see the message and will have to press the Enter key to get the menu shown again.
And a case of use of this would be:
1) Option 1
2) Option 2
3) Option 3
4) Exit
Please enter your choice: 1
you chose choice 1 which is Option 1
Press [Enter] key to continue...
Upvotes: 1
Reputation: 531430
If the user hits enter without making any selection, the menu will be redisplayed. As far as I know, the only way to programmatically force the menu to be drawn again would be to wrap the select
statement in another loop, and re-execute the select
statement. You'll use break
to exit the select
and return to the top of the wrapper loop, and break 2
to exit from both the select
and the wrapper.
PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Option 4")
while :; do
select opt in "${options[@]}"
do
case $opt in
"Option 1")
echo "you chose choice $REPLY which is $opt"
;;
"Find Subdomain")
echo "you chose choice $REPLY which is $opt"
;;
"Option 3")
echo "you chose choice $REPLY which is $opt"
;;
"Option 4")
break 2
;;
*) echo "invalid option $REPLY";;
esac
break
done
done
Upvotes: 1