repeat command until keypressed?

Karl Auer kauer at biplane.com.au
Fri Oct 23 12:21:47 UTC 2009


On Fri, 2009-10-23 at 04:41 -0700, Eugeneapolinary Ju wrote:
> How can I write a bash script, that will repeat the commands
> until any key is pressed?

This little script will wait one second for you to press a key (has to
be a character key, not something like shift), and if you don't it just
loops.


#!/bin/bash
while [ true ] ; do
   read -t 1 -n 1
   if [ $? = 0 ] ; then
      exit ;
   else
      echo waiting...
   fi
done



"read" returns zero if input was received, non-zero if the input times
out.

"-t 1" says wait only one second for input before timing out.

"-n 1" says accept a single character of input, don't wait for a whole
line.

Very important point: This really has to be bash! The simplified read in
dash (the default Linux shell) is not good enough.

Regards, K.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)                   +61-2-64957160 (h)
http://www.biplane.com.au/~kauer/                  +61-428-957160 (mob)

GPG fingerprint: 07F3 1DF9 9D45 8BCD 7DD5 00CE 4A44 6A03 F43A 7DEF

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20091023/3e00df02/attachment.sig>


More information about the ubuntu-users mailing list