Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to end while loop with key pressed?

924 views
Skip to first unread message

janmartin

unread,
Mar 14, 2011, 7:10:43 AM3/14/11
to
Hi all,

I need to write an infinite loop that stops when any key is pressed.

Unfortunately this one loops only when a key is pressed.

Ideas please?

#!/bin/bash

count=0
while : ; do

# dummy action
echo -n "$a "
let "a+=1"

# detect any key press
read -n 1 keypress
echo $keypress

done
echo "Thanks for using this script."
exit 0

Ralf Goertz

unread,
Mar 14, 2011, 9:00:26 AM3/14/11
to
janmartin wrote:

> Hi all,
>
> I need to write an infinite loop that stops when any key is pressed.
>
> Unfortunately this one loops only when a key is pressed.
>
> Ideas please?

> …
> read -n 1 keypress
> …

Try this one instead

read -t 0.5 -n 1 keypress && exit

0 new messages