I want to, from a bash shell script, present the user with a menu of choices to pick from. The selection should then be stored in an environment variable for further use later.
If I run from the command line:
dialog \ --backtitle "Make a choice" \ --menu "Take a pick" \ 10 40 3 \ choice1 "First choice" \ choice2 "Second choice"
then everything works fine; I get the menu that I want, and
choice2 is printed back at me. However, if instead I run:
SELECTION=$( dialog \ --backtitle "Make a choice" \ --menu "Take a pick" \ 10 40 3 \ choice1 "First choice" \ choice2 "Second choice" \ )
then it no longer works the way I want to.
- No menu is displayed, although (as far as I can tell)
dialogexits when I press Enter
- When I exit the non-displayed menu by pressing Enter, the tag of the selection appears on the terminal rather than being stored into $SELECTION
I have tried adding
2>&1 to redirect stderr to stdout, in case it really was being printed to stderr instead. The only noticable effect of that is that now, nothing is returned, either from the command substitution expression or printed to the terminal.
On my system,
cdialog 1.1-20120215. The
dialog man page says about
--menu that On exit the tag of the chosen menu entry will be printed on dialog's output. This seems like it should work fine with something like the above, but it doesn't for me.
I want the menu to be displayed, allowing the user to pick between the available choices, and when the user accepts, the tag (for example,
choice2) for the selected choice to be stored in the environment variable $SELECTION. If the user cancels, there should be clear indication of that (ideally, $SELECTION would be undefined or a zero-length string).
How can I do that? What piece of the puzzle am I missing?
Solutions suggesting alternatives to
dialog are also acceptable, as long as they present a nice menu to choose from and ideally the software suggested isn't too exotic (availability in the Debian Stable
main repository is a requirement).