Mailinglist Archive: opensuse (2348 mails)

< Previous Next >
Re: [opensuse] Quick bash question - testing for the presence of keyboard input without pausing a loop?
  • From: G T Smith <grahamsmith@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
  • Date: Sat, 05 Apr 2008 08:44:16 +0100
  • Message-id: <47F72DD0.4000507@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
Hash: SHA1

David C. Rankin wrote:

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

while [ -f "/home/david/.runmcelog" ]; do
sudo /usr/sbin/mcelog --k8

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
My wish has come true. I no longer know how to use my telephone.

Bjarne Stroustrup
Version: GnuPG v1.4.5 (GNU/Linux)
Comment: Using GnuPG with SUSE -

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

< Previous Next >
This Thread