[opensuse] Quick bash question - testing for the presence of keyboard input without pausing a loop?
Listmates, I'm stuck. I would like to write a bash routine that tests for the presence of a user keypress ('q' or 'e') to exit a loop from within the loop without stopping the loop to wait like with read. Basically just execute the loop until the user presses a key, any key, to stop it. I can't figure out how do that will a keypress. As a work around, I'm using a basic file ".runmcelog" file for that purpose: while [ -f "/home/david/.runmcelog" ]; do sudo /usr/sbin/mcelog --k8 done Which requires that I delete and touch .runmcelog to stop and reset the script, but there has to be a better way. What says the braintrust on this issue? -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
----- Original Message -----
From: "David C. Rankin"
Listmates,
I'm stuck. I would like to write a bash routine that tests for the presence of a user keypress ('q' or 'e') to exit a loop from within the loop without stopping the loop to wait like with read. Basically just execute the loop until the user presses a key, any key, to stop it. I can't figure out how do that will a keypress.
As a work around, I'm using a basic file ".runmcelog" file for that purpose:
while [ -f "/home/david/.runmcelog" ]; do sudo /usr/sbin/mcelog --k8 done
Which requires that I delete and touch .runmcelog to stop and reset the script, but there has to be a better way. What says the braintrust on this issue?
simplest option: Press Ctrl-C to stop instead of q/e/other sudo watch -n 1 mcelog --k8 or: while : ;do sudo /usr/sbin/mcelog --k8 ;done Next simplest option: Timed read. Press any key to stop (not just q or ctrl-c). Waits 1 second per iteration. This read syntax is bash-specific. unset REPLY while [ -z "$REPLY" ] ;do sudo /usr/sbin/mcelog --k8 read -s -n 1 -t 1 done Or if you really want to catch only the q or e: (Ctrl-C still works too) unset REPLY while : ;do sudo /usr/sbin/mcelog --k8 read -s -n 1 -t 1 case "$REPLY" in q|e) break ;; esac done You might be able to get fancy by binding the q key to a signal using bash's readline features, and then using trap to catch that signal, with no read command in the loop. That way would allow a full-speed loop (no 1 second pause). -- Brian K. White brian@aljex.com http://www.myspace.com/KEYofR +++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++. filePro BBx Linux SCO FreeBSD #callahans Satriani Filk! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 David C. Rankin wrote:
Listmates,
I'm stuck. I would like to write a bash routine that tests for the presence of a user keypress ('q' or 'e') to exit a loop from within the loop without stopping the loop to wait like with read. Basically just execute the loop until the user presses a key, any key, to stop it. I can't figure out how do that will a keypress.
As a work around, I'm using a basic file ".runmcelog" file for that purpose:
while [ -f "/home/david/.runmcelog" ]; do sudo /usr/sbin/mcelog --k8 done
Which requires that I delete and touch .runmcelog to stop and reset the script, but there has to be a better way. What says the braintrust on this issue?
Control-C usually will stop a running script. And you can use trap to deal with any tidying up afterwards... Not really what trap is intended for but it should work...syntax below... trap <command> <signal> ... Control-C generates an INT signal.... There are some other possibilities if you do not want screen output from the primary process. Bash is not really designed for this kind of processing, AFAIK the bash read function cannot be gimmicked to act as a get-like function (but I have never looked for this in a bash script)... If you want this kind of functionality it might be worth looking at another scripting language. - -- ============================================================================== I have always wished that my computer would be as easy to use as my telephone. My wish has come true. I no longer know how to use my telephone. Bjarne Stroustrup ============================================================================== -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org iD8DBQFH9y3PasN0sSnLmgIRAt00AJ9iQaSG/V0s6FQkEE1ChnCUkj4CIwCfbe6U ZmQweXlmy9fh3ftqBij7GCk= =6qTg -----END PGP SIGNATURE----- -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
David C. Rankin wrote:
Listmates,
I'm stuck. I would like to write a bash routine that tests for the presence of a user keypress ('q' or 'e') to exit a loop from within the loop without stopping the loop to wait like with read. Basically just execute the loop until the user presses a key, any key, to stop it. I can't figure out how do that will a keypress.
As a work around, I'm using a basic file ".runmcelog" file for that purpose:
while [ -f "/home/david/.runmcelog" ]; do sudo /usr/sbin/mcelog --k8 done
I would fork off the rest of the processing code in the background. Like this run_processing_code & (by moving it to a new shell script... all of your variable and environment WILL be inherited) The new shell does the processing: and either runs a loop while [ ! -f $STOPFILE ] do something done or more simply: cleanup_func() { clean-up code goes here } trap $MYSIG cleanup_func() for (;;) do something done # cleanup_func() will actually execute HERE exit Or you can be more sophisticated, and build a signal catcher using the built-in command trap ...but that's probably overkill for this. The syntax for trap is like this trap signo action Meanwhile, the original shell does this: CHILD=$! READ $A touch $STOPFILE # or send a signal to using: # kill -$MYSIG $CHILD rm -f $STOPFILE # clean up our mess, ignore non-existance errors exit -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Sam Clemens wrote:
David C. Rankin wrote:
Listmates,
I'm stuck. I would like to write a bash routine that tests for the presence of a user keypress ('q' or 'e') to exit a loop from within the loop without stopping the loop to wait like with read. Basically just execute the loop until the user presses a key, any key, to stop it. I can't figure out how do that will a keypress.
As a work around, I'm using a basic file ".runmcelog" file for that purpose:
while [ -f "/home/david/.runmcelog" ]; do sudo /usr/sbin/mcelog --k8 done
I would fork off the rest of the processing code in the background.
Like this
(sorry Sam, you'll get two copies.. oops.) Bingo! You hit the nail on the head. Thanks to all that responded. Now I have "several" more arrows to add to my quiver.... -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (4)
-
Brian K. White
-
David C. Rankin
-
G T Smith
-
Sam Clemens