Multi-select menu in bash script

Posted by am2605 on Server Fault See other posts from Server Fault or by am2605
Published on 2010-05-25T03:51:30Z Indexed on 2010/05/25 4:02 UTC
Read the original article Hit count: 504

Filed under:
|

I'm a bash newbie but I would like to create a script in which I'd like to allow the user to select multiple options from a list of options.

Essentially what I would like is something similar to the example below:

       #!/bin/bash
       OPTIONS="Hello Quit"
       select opt in $OPTIONS; do
           if [ "$opt" = "Quit" ]; then
            echo done
            exit
           elif [ "$opt" = "Hello" ]; then
            echo Hello World
           else
            clear
            echo bad option
           fi
       done

(sourced from http://www.faqs.org/docs/Linux-HOWTO/Bash-Prog-Intro-HOWTO.html#ss9.1)

However my script would have more options, and I'd like to allow multiples to be selected. So somethig like this:

1) Option 1 2) Option 2 3) Option 3 4) Option 4 5) Done

Having feedback on the ones they have selected would also be great, eg plus signs next to ones they ahve already selected. Eg if you select "1" I'd like to page to clear and reprint:

1) Option 1 +
2) Option 2
3) Option 3
4) Option 4
5) Done

Then if you select "3":

1) Option 1 +
2) Option 2
3) Option 3 +
4) Option 4
5) Done

Also, if they again selected (1) I'd like it to "deselect" the option:

1) Option 1
2) Option 2
3) Option 3 +
4) Option 4
5) Done

And finally when Done is pressed I'd like a list of the ones that were selected to be displayed before the program exits, eg if the current state is:

1) Option 1
2) Option 2 +
3) Option 3 + 
4) Option 4 +
5) Done

Pressing 5 should print:

Option 2, Option 3, Option 4

and the script terminate.

So my question - is this possible in bash, and if so is anyone able to provide a code sample?

Any advice would be much appreciated.

© Server Fault or respective owner

Related posts about bash

Related posts about menu