Con ddte puoi leggere in modo affidabile un singolo byte da un file. Con sttyè possibile impostare un minnumero di byte per qualificare una lettura del terminale e timeun'uscita in decimi di secondo. Combina quei due e ne puoi fare a meno sleep, penso, e lascia che il timeout di lettura del terminale faccia il lavoro per te:
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
Questo è un piccolo esempio whileche ho preso in giro per farti provare. Ogni due secondi ddscade il tentativo di leggere stdin- reindirizzato da /dev/tty- e il whileloop scorre. Questo o dd non si interrompe perché si preme un tasto, nel qual caso viene invocata una shell interattiva.
Ecco una prova: i numeri stampati all'inizio di ogni riga rappresentano il valore della variabile 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 premo CTRL + Z per fermarmi, fattinotify-send helloeseguire. se il secondo comando viene eseguito come mai il primo processo viene interrotto? dopo di che, se nonfgriesco a vedere nulla, è ovvio, dato che il secondo comando è già eseguito