Operating System - HP-UX
1834043 Members
2299 Online
110063 Solutions
New Discussion

Re: Exit loop with any keystroke

 
SOLVED
Go to solution
Brian K. Arnholt
Frequent Advisor

Exit loop with any keystroke

I have been reading the forums looking for a solution, searching on trap, stty... but I am at a loss...here's my issue:

I have a simple script that enters into a continous loop and runs a program to dipslay the contents of a file to the screen. I know I can exit out of the loop by hitting Ctrl-C, etc.

What I would like to do is exit the loop when a user presses ANY key, not just return, or Ctrl-C, etc.

Is there a way to do this?
Some see things as they are and ask why, I dream of things that never were and ask why not?
4 REPLIES 4
Mark Greene_1
Honored Contributor

Re: Exit loop with any keystroke

You can try this:

stty raw;char=`dd if=/dev/tty count=1 2>/dev/null`;stty -raw


mark
the future will be a lot like now, only later
Kent Ostby
Honored Contributor

Re: Exit loop with any keystroke

Dug around on google and hacked together this.

Only I seem to have to hit 3 keystrokes to get it to quit.

Might give you some ideas.

Basically save the stty settings, send a message, turn off echo and -icanon and then have dd wait for an input character then restore the old stty settings:

old_tty_setting=$(stty -g)
echo "Hit key to break loop "
stty -icanon -echo
usethis=$(dd bs=1 count=1 of=/dev/null)
stty $old_tty_setting

Hope that helps or maybe someone can suggest why it would take multiple characters.
"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
James A. Donovan
Honored Contributor
Solution

Re: Exit loop with any keystroke

Check this link. Good example script and explanation.

http://www.dbaoncall.net/references/ex_one_char.html
Remember, wherever you go, there you are...
Brian K. Arnholt
Frequent Advisor

Re: Exit loop with any keystroke

Thanks to all, Jim's link was the cleanest.
Some see things as they are and ask why, I dream of things that never were and ask why not?