User input using a BASH script...
Hey all,
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: " # read the menu selection get_choise # print the results. case $MENU_CHOISE in "1") echo "Can I see some ID?";; "2") echo "Smoking is bad for you";; "3") echo "One lump or two?";; esac |
good tutorial
http://www.linux.org/docs/ldp/howto/...10.html#ss10.1
thats the link for the user input page, it may be worthwhile you reading the whole thing though, it is a good tutorial |
Thanks for taking the trouble in helping me suss this out.
|
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 |
All times are GMT -5. The time now is 06:58 AM. |