Pause Bash loop

I have a bash script. How can I pause when pressed or pause after several cycles, but if I do not press a single key, should it loop?

for i in `cat files` 
do
    echo $i
done
+3
source share
1 answer

I think I initially misunderstood your question. You can pause the active process at any time by pressing Ctrl+Zand resume it with fg .

To pause the script after performing several iterations, you can use the counter variable and the modulo operator %:

i=1
for f in `cat files`; do
    echo $f
    if (( i % 10 == 0 )); then  # pause every 10 iterations
        read
    fi
    let "i++"
done

My initial answer is:

read, , RETURN ( EOF, Ctrl+D):

for i in `cat files`
do
    echo $i
    read
done

-t read, - :

for i in `cat files`
do
    echo $i
    read -t 1
done

1 , RETURN.

+4

Source: https://habr.com/ru/post/1784240/


All Articles