Global variable in for loop (BASH)


 
Thread Tools Search this Thread
Top Forums Programming Global variable in for loop (BASH)
# 1  
Old 01-20-2014
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.

Code:
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


Last edited by shadyuk; 01-20-2014 at 06:39 AM..
# 2  
Old 01-20-2014
Quote:
Originally Posted by shadyuk
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.

Code:
pause=0
user=1

(for (( ; ; ))
do
   speed=`cat speed.log`
   if [ "$speed" -lt 10 ]; then
      pause=1
   else
      pause=0
   fi
sleep 30
done) &

function 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 ?

Last edited by Akshay Hegde; 01-20-2014 at 06:13 AM..
# 3  
Old 01-20-2014
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.
# 4  
Old 01-20-2014
Quote:
Originally Posted by shadyuk
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.

Code:
pause=0
user=1

(for (( ; ; ))
do
   speed=`cat speed.log`
   if [ "$speed" -lt 10 ]; then
      pause=1
   else
      pause=0
   fi
sleep 30
done) &

function 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


Where did you define function one_user
Where is variable $users

Please post your real code.. Is it typo again ?
# 5  
Old 01-20-2014
Amended above. I haven't constructed the entire code yet as I'm testing snippets of it before piecing it all together.
# 6  
Old 01-20-2014
Ok.. I created one test file named speed.log which looks as below

Code:
$ cat speed.log 
5

Code:
#!/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

Code:
$ 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.
This User Gave Thanks to Akshay Hegde For This Post:
# 7  
Old 01-20-2014
Thanks. This should work although i was hoping for a solution without writing to file.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash Variable scope - while loop while reading from a file

Cope sample1: test.sh i=0 echo " Outside loop i = $i " while do i=$(( $i + 1)) echo "Inside loop i = $i " done echo " Out of loop i is : $i " When run output : Outside loop i = 0 Inside loop i = 1 Inside loop i = 2 Inside loop i = 3 Inside loop i = 4 Inside loop i = 5 Inside... (8 Replies)
Discussion started by: Adarshreddy01
8 Replies

2. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

3. Shell Programming and Scripting

Bash variable available for use outside loop

In the below for loop, I extract a variable $d which is an id that will change each time. The bash executes the problem that I am having is that p (after the done) is the path with the extracted $d. However, I can not use it in subsequent loops as it is not reconized. I have been trying to change... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Unable to set Global variable outside while loop

Below is my code: count=0 if ... ... else ... find * -prune -type d | sort -r -n | while read d; do count=1 if ; then echo "Count1:$count" ... ... break 2; fi ... done ... fi echo "Count2:$count" (9 Replies)
Discussion started by: mohtashims
9 Replies

5. Shell Programming and Scripting

Bash for loop with arrays second variable?

I am fairly new to bash and am not sure how to resolve this: I have a series of geographical long/lat points eg. 50/-30 listed on separate lines in a file called junk2. I have input these into an array and am then using that array in a for loop. Towards the end of the loop I create a file called... (4 Replies)
Discussion started by: lily-anne
4 Replies

6. Shell Programming and Scripting

Detail on For loop for multiple file input and bash variable usage

Dear mentors, I just need little explanation regarding for loop to give input to awk script for file in `ls *.txt |sort -t"_" -k2n,2`; do awk script $file done which sorts file in order, and will input one after another file in order to awk script suppose if I have to input 2 or... (4 Replies)
Discussion started by: Akshay Hegde
4 Replies

7. UNIX for Dummies Questions & Answers

Help with 3 variable bash loop

Hi all! I think someone might be able to solve my problem pretty easily. I am trying to run a bash loop with 3 variables. I know how to do: for var1 in `cat list1`; do for var2 in `cat list2`; do for var3 in `cat list3`; command var1 var2 > var3; done; done; done However, this will run all... (4 Replies)
Discussion started by: torchij
4 Replies

8. Shell Programming and Scripting

(BASH) Using a loop variable to grep something in a file?

Hi, I have a loop running until a variable L that is read previously in the full script. I'd like to grep some information in an input file at a line that contains the value of the loop parameter $i. I've tried to use grep, but the problem is nothing is written in the FILE files. It seems grep... (5 Replies)
Discussion started by: DMini
5 Replies

9. Shell Programming and Scripting

Global variable value

Hi All, Im new to shell scripting. I am running EgA.sh and setting one global variable XYZ=0 . Also calling another EgB.sh from EgA.sh, changing the value of XYZ=10 but after executing EgB.sh, value of XYZ is still 0. Im expecting it to be 10. Anyone for help. Thanks in Advance. :) (5 Replies)
Discussion started by: paliwal
5 Replies

10. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies
Login or Register to Ask a Question