Mailinglist Archive: opensuse (2348 mails)

< Previous Next >
Re: [opensuse] Quick bash question - testing for the presence of keyboard input without pausing a loop?

----- Original Message -----
From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
To: "suse" <opensuse@xxxxxxxxxxxx>
Sent: Saturday, April 05, 2008 2:45 AM
Subject: [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?


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@xxxxxxxxx http://www.myspace.com/KEYofR
+++++[>+++[>+++++>+++++++<<-]<-]>>+.>.+++++.+++++++.-.[>+<---]>++.
filePro BBx Linux SCO FreeBSD #callahans Satriani Filk!


--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
This Thread
References