Global variable in for loop (BASH)

Hello,

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

Correct it

while [ "$pause" -eq 1 ]; do

--edit---

I didn't understand what's your aim ? where you are breaking loop ?

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.

Where did you define function one_user
Where is variable $users

Please post your real code.. Is it typo again ?

Amended above. I haven't constructed the entire code yet as I'm testing snippets of it before piecing it all together.

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.

Thanks. This should work although i was hoping for a solution without writing to file.

The pause you're setting in the for loop is not the same variable as the one you're checking in the function - since it's in a sub-shell.

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

Also, be aware that you're generating orphaned processes every time your script runs.