PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Catch Keypress To Stop A Timer In Bash On Linux

Go down

Catch Keypress To Stop A Timer In Bash On Linux Empty Catch Keypress To Stop A Timer In Bash On Linux

Post by jamied_uk 23rd May 2016, 14:24

Code:
#!/bin/bash
# Usage ./test.sh
if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi

count=0
keypress=''
while [ "x$keypress" = "x" ]; do
  let count+=1
  echo -ne $count'\r'
  keypress="`cat -v`"
done

if [ -t 0 ]; then stty sane; fi

echo "You pressed '$keypress' after $count loop iterations"
echo "Thanks for using this script."
exit 0

# since=$(date +%s);watch -tn 1 eval "echo \$((\$(date +%s) - $since))"

To make executable
Code:
sudo chmod +x test.sh
jamied_uk
jamied_uk
Admin

Posts : 2950
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum