How to make shell script wait for a particular key pressed in order to proceed?
Hello,
This is a question for shell scripting. Uhmm i've been searching all over the internet for a way to let the user press a particular key let's say 'y' without the user having to press [Enter] as confirmationfor yes, this would then run another function. like say.. Quote:
I understand the "press any key to continue" would go something like Code:
read -sn 1 -p "Press any key to continue.." |
Try something like
Code:
prompt='Press n for no or y for yes ' |
Quote:
as my program's menu structure goes like this.. Book Inventory Program 1) Add book 2) Remove book 3) Update book 4) Search book 5) Process book sold 6) Inventory Summary Report <<-- This is where the "press a particular key" thing is implemented. 7) Quit Anyway below is my function just in case you guys need to see it. It's pretty messy and i'm not sure if there's any way i could save the awk printout into a file without having to copy the whole awk command again for file saving process. :) Code:
function inventory_summary_report |
Quote:
Unfortunately bash does not have a non-blocking read facility so there's no way to empty the terminal input (keyboard) buffer. It could be done by writing a custom "external command" (that's bash-speak) as mentioned here. A version of bash later than the 3.1.7 used for testing introduced fractions of a second on read's -t option. GNU Bash Reference says this about read: "-t timeout Cause read to time out and return failure if a complete line of input is not read within timeout seconds. timeout may be a decimal number with a fractional portion following the decimal point." This could be used with a low timeout value to approximate a non-blocking read, something like this to go at the top of the * case code above (it's a bit ugly, maybe putting a string in a variable called char!) Code:
while read -t '0.01' -n1 buf |
Ahhh i see, well it didn't work out the way i hope it would, but thank you very much for your help catkin! :)
I'll still use the case thing but will keep my fingers crossed hoping the the person assessing my assignment ain't gonna hit the arrow keys. hahaha. |
small change to catkins code...
Code:
while true |
All times are GMT -5. The time now is 04:38 PM. |