@rcrsn51
Are you still interested in understanding why the "done" method fails?
Yes
Do like your code
Code: Select all
for ((i=1; i<=15; i++)) { echo $i; sleep 1; } &
PID=$!
sleep 10
kill $PID
My original code attempts to stop the loop with the if statement inside the loop, yours just kills the process which stops the loop.
Many of the replie above don't follow the intent of the original code which should have been explained better. Pseudo code for it would be something like:
-Start a timer
-Run a process
-Stop the timer when the process is complete
Have not found a good answer for these two cases.
In this case the variable "done" is defined and read from outside the loop and stops the loop.
Code: Select all
#! /bin/bash
done=1
for ((i=1; i<=15; i++)) {
if [[ $done == 1 ]];then
break
fi
echo $i
sleep 1
}
Whereas, if the variable is incremeted after the loop is running it isn't read and does not work.
Code: Select all
#! /bin/bash
done=0
for ((i=1; i<=15; i++)) {
if [[ $done == 1 ]];then
break
fi
echo $i
sleep 1
} &
sleep 10
done=1
echo $done
echo done=1
Thanks
wizard