ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm trying to write a menu in BASH that would accept a user's input with one kestroke. For instance, if the menu contained these options:
1) Beer
2) Cigarettes
3) Coffee
... a user could simply type the number three without having to press the enter key for the script to read the input. I've read through a couple of tutorials and they use the "echo -n" and "read" combination in their scripts which isn't what I want to do. I would like to minimize the amount of keystrokes a user would have to make.
I also happened upon the dialog utility to make text boxes for menus. Will this help in creating the one-keystroke functionality I'd like to have?
To change settings on in your term you have to make changes the terminal driver. You can do this by using the "stty" command. I wasn't quite sure how to read one character from the stdin so I googled for it and it seems to work well.
Hope this helps,
cludwin
#!/bin/sh
# global var to hold menu selection
MENU_CHOISE=""
function get_choise() {
##################################
# set the tty driver to raw mode #
# read a char #
# restore settings #
##################################
stty raw
MENU_CHOISE=`dd if=/dev/tty bs=1 count=1 2>/dev/null`
stty sane
echo $MENU_CHOISE
}
# print the menu
echo "Menu:"
echo " 1) booz"
echo " 2) smokes"
echo " 3) tea"
printf " please enter your choise: "
I'd really like to use Dialog and BASH if I can, however.
I have two questions concerning dialog:
Firstly, how do I get two widgets operating on the same screen? Is it even possible? I'd like to have a menu on the left-hand side containing a list of items with a running total on the right-hand side.
Secondly, where does the data from the menu go? How do I assign it to a variable that would affect the running totals on the right hand side? Reading the man pages for Dialog, output from a menu goes to something called stderr. I would need to add the dollar value of each selected item, have that value listed in the "Running total" widget on the right-hand side, and then possibly save everything to a file.
Am I better trying to do this using only BASH? Should I take a look at python-tcl/tk?
AFAIK you can't use multiple widgets using Dialog.
I'm trying to write a menu in BASH that would accept a user's input with one kestroke.
Maybe "select"? Useless example:
select choice in quit bomb capitulate educate freeze jump k-line maim nuke obliterate submit tundra zygote; do case "$choice" in quit) echo To ${lastchoice} or not to ${lastchoice}, that is the $MYNAME dinarii question...; break;; *) lastchoice=$choice; export MYNAME=$(expr $MYNAME + $RANDOM);; esac; done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.