Just how can I create a select food selection in a covering script?

I'm developing a straightforward bash script and also I intend to create a select food selection in it, similar to this:


echo "Choose your option:"

1) Option 1  
2) Option 2  
3) Option 3  
4) Quit  

And also according to customer's selection, I desire various activities to be implemented. I'm a bash covering scripting noob, I've looked the internet for some solutions, yet obtained absolutely nothing actually concrete.

2019-05-05 10:19:17
Source Share
Answers: 4

There is currently the very same inquiry in serverfault addressed. The remedy there makes use of whiptail.

2019-05-08 11:02:57

Assuming you intend to make use of a simple shell manuscript food selection (no fancy UI), examine the food selection instance from http://www.tldp.org/LDP/abs/html/testbranch.html.

2019-05-08 10:54:08
# Bash Menu Script Example

PS3='Please enter your choice: '
options=("Option 1" "Option 2" "Option 3" "Quit")
select opt in "${options[@]}"
    case $opt in
        "Option 1")
            echo "you chose choice 1"
        "Option 2")
            echo "you chose choice 2"
        "Option 3")
            echo "you chose choice $REPLY which is $opt"
        *) echo "invalid option $REPLY";;

Add break declarations any place you require the select loop to exit. If a break is not executed, the select declaration loopholes and also the food selection is re - presented.

In the 3rd alternative, I consisted of variables that are set by the select declaration to show that you have accessibility to those values. If you pick it, it will certainly result :

you chose choice 3 which is Option 3

You can see that $REPLY has the string you got in at the prompt. It is made use of as an index right into the array ${options[@]} as if the array were 1 based. The variable $opt has the string from that index in the array.

Keep in mind that the selections can be a straightforward checklist straight in the select declaration similar to this :

select opt in foo bar baz 'multi word choice'

yet you can not place such a checklist in a scalar variable as a result of the rooms in among the selections.

You can additionally make use of documents globbing if you are picking amongst documents :

select file in *.tar.gz
2019-05-08 06:09:18

Since this is targeted at Ubuntu you need to make use of whatever backend debconf is set up to make use of. You can figure out the debconf backend with :

sudo -s "echo get debconf/frontend | debconf-communicate"

If it claims "dialog" after that it most likely usages whiptail or dialog. On Lucid it's whiptail.

If that falls short, make use of bash "select" as clarified by Dennis Williamson.

2019-05-07 23:08:55