Com ddvocê, você pode ler com segurança um único byte de um arquivo. Com sttyvocê pode definir um minnúmero de bytes para qualificar uma leitura de terminal e uma timesaída em décimos de segundo. Combine esses dois e você pode ficar sem sleeptotalmente, eu acho, e deixe o tempo limite de leitura do terminal fazer o trabalho para você:
s=$(stty -g </dev/tty)
(while stty raw -echo isig time 20 min 0;test -z "$(
dd bs=1 count=1 2>/dev/null; stty "$s")" || (exec sh)
do echo "$SECONDS:" do your stuff here maybe
echo no sleep necessary, I think
[ "$((i+=1))" -gt 10 ] && exit
done
) </dev/tty
Esse é um pequeno exemplo de whileloop que eu criei para você experimentar. A cada dois segundos, o ddtempo limite é excedido na tentativa de leitura stdin- redirecionada de /dev/tty- e o whileloop é repetido. O tempo limite é excedido ou dd não porque você pressiona uma tecla - nesse caso, um shell interativo é chamado.
Aqui está uma execução de teste - os números impressos no início de cada linha são o valor da variável shell $SECONDS:
273315: do your stuff here maybe
no sleep necessary, I think
273317: do your stuff here maybe
no sleep necessary, I think
273319: do your stuff here maybe
no sleep necessary, I think
273321: do your stuff here maybe
no sleep necessary, I think
sh-4.3$ : if you press a key you get an interactive shell
sh-4.3$ : this example loop quits after ten iterations
sh-4.3$ : or if this shell exits with a non-zero exit status
sh-4.3$ : and speaking of which, to do so you just...
sh-4.3$ exit
exit
273385: do your stuff here maybe
no sleep necessary, I think
273387: do your stuff here maybe
no sleep necessary, I think
273389: do your stuff here maybe
no sleep necessary, I think
273391: do your stuff here maybe
no sleep necessary, I think
273393: do your stuff here maybe
no sleep necessary, I think
273395: do your stuff here maybe
no sleep necessary, I think
273397: do your stuff here maybe
no sleep necessary, I think
sleep 10; notify-send helloe pressionar CTRL + Z para parar,notify-send helloseja executado. se o segundo comando está sendo executado, como é que o primeiro processo é interrompido? depois disso, se o tipofgeu não posso ver acontecendo nada, o que é óbvio, já que segundo comando já é executado