I'm trying to read the variable "pause" from a for loop without luck. The function is dependant on the outcome of the test within the loop. If i run this, pause is always 0 within the function. Any ideas?
Thanks.
pause=0
users=1
(for (( ; ; ))
do
speed=`cat speed.log`
if [ "$speed" -lt 10 ]; then
pause=1
else
pause=0
fi
sleep 30
done) &
function one_user () {
local user=$1
while [[ "$pause" = 1 ]]; do
echo pausing script
sleep 10
done
# else continue
}
for (( user = 1; user <= $users; user++ )); do
one_user $user &
done
Sorry, that was just a typo. Even so, what you're suggesting doesn't solve the problem.
---------- Post updated at 05:18 AM ---------- Previous update was at 05:11 AM ----------
I'm not breaking the loop. My script is reading the GPS speed every 30s and if the speed is less than 10, i want whatever is within the subroutine (function) to stop. Once the speed exceeds 10, it will get going again. The problem is that the variable "pause" isn't being updated within the subroutine.#
I should add that I don't want to put the test itself within the function. I've simplified the code above on purpose and there's an additional component within the for loop that counts the number of times the speed is less than 10. So, the structure must remain intact.
Ok.. I created one test file named speed.log which looks as below
$ cat speed.log
5
#!/bin/bash
pause=0
users=10
for (( ; ; ));do
speed=$(cat speed.log)
[ "$speed" -lt 10 ] && echo 1 || echo 0
sleep 1
done >/tmp/myid &
function user(){
pause=$(cat /tmp/myid)
while [[ "$pause" = 1 ]]; do
echo pausing script $(date)
sleep 2
done
}
for(( user = 1; user <= $users; user++ )); do
user
done
$ bash test.sh
pausing script Mon Jan 20 16:40:42 IST 2014
pausing script Mon Jan 20 16:40:44 IST 2014
pausing script Mon Jan 20 16:40:46 IST 2014
pausing script Mon Jan 20 16:40:48 IST 2014
pausing script Mon Jan 20 16:40:50 IST 2014
pausing script Mon Jan 20 16:40:52 IST 2014
pausing script Mon Jan 20 16:40:54 IST 2014
pausing script Mon Jan 20 16:40:56 IST 2014
pausing script Mon Jan 20 16:40:58 IST 2014
......
Hope you can implement now according to your need
change sleep seconds and remove $(date) in echo statement, as I added it for testing purpose.