Portál AbcLinuxu, 12. května 2025 01:59
read -t0
le otestuje, ci je vstupny buffer neprazdny. Takze ho mozes vycistit cyklom s read -t0 x && read -n1 x
a az potom spustit ostry read pre data od usera. Ale nenapada ma, ako to dobre otestovat, pretoze to funguje iba pri citani z terminalu, pipy a pod.
while true do if [[ -t read -t0 le ]] then read -t0 x && read -n1 x else break fi done
while true; do if read -t0 x; then read -n1 x else break fi done # mame cisty bufferAle ci to funguje, to si musi otestovat ten, kto to bude pouzivat. Este mozno to
-n1
je tam asi zbytocne - netreba cistit po jednom znaku, moze sa po riadkoch.
while true; do if read -t0 x; then read -n1 x else break fi done
#!/bin/bash input1='' echo "Input 1:" read input1 echo "Write anything:" sleep 5 input2='' read -t 1 -n 255 echo "Input 2:" read input2 echo "Input 1: ${input1}" echo "Input 2: ${input2}"
set -x; sleep 5; while read -t 0.1 -N 1 aha; do true; done; read -p ">" pokus; echo "'$pokus'"(to "set -x" způsobuje vypisování příkazů při provádění - vypíná se to "set +x" a na funkci to nemá vliv)
> read -t 0.1 -bash: read: 0.1: invalid timeout specification > read -N 1 -bash: read: -N: invalid option read: usage: read [-ers] [-u fd] [-t timeout] [-p prompt] [-a array] [-n nchars] [-d delim] [name ...] > read -t 0.1 -N 1 -bash: read: 0.1: invalid timeout specification > read -t 1 -n 1 −> OKToš tak
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.