I have the following Bash script. How can I make it pause on keypress or pause after a number of loops, but if I don't press any key it should loop?
for i in `cat files`
do
echo $i
done
I think I initially misread your question. You can suspend the active process at any time by pressing Ctrl+Z, and resume it with the fg builtin.
In order to make the script pause after a number of iterations have been performed, you can use a 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 original answer was:
You can use the read builtin to have the shell wait until the user presses the RETURN key (or the EOF key, Ctrl+D):
for i in `cat files`
do
echo $i
read
done
You can use the -t option of read in order have it time out and continue execution:
for i in `cat files`
do
echo $i
read -t 1
done
The above will resume execution after 1 second if the user doesn't press the RETURN key.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With