Welcome to Linux Forums!

With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.

Linux Forum ArticlesLinux ForumsLinux Forum DownloadsLinux Hosts
Home|Register|FAQ|Member List|Calendar|Unanswered Posts|Forum Rules|Today's Posts|Advanced Search|
SEARCH FOR IN
Go Back   Linux Forums > GNU Linux Zone > Linux Programming & Scripting
Reload this Page Setting global variable from within a loop
Linux Forums
Linux Forums
Welcome To The Linux Forums!
Welcome to Linux Forums. We pride ourselves in being one of the largest Linux communities on the web, we encourage you to REGISTER on our forums and participate in the community. There are over 150,000 members ready to answer your questions. JOINING US today will allow you to make new posts, get support, send messages to other members and submit downloads to our downloads directory and many other great features!

Linux Programming & Scripting C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Reply
 
Thread Tools Display Modes
Old 4 Weeks Ago   #1 (permalink)
ajauberg
Just Joined!
 
Join Date: Jul 2008
Posts: 4
Setting global variable from within a loop

Hi,

I am creating a script that collects data from remote routers and reports this back to a central location. I do however have a problem with constructing the URL that shall be reported back because the global variable WGET which is used to build the string that shall be reported back is not treated as global, but is only updated locally within the loop:

Code:
#!/bin/sh

WIFI=ath0
BR=br0

# Update the MAC, IP address and peers of the AP
WGET=http://<mysite>/update2.php

IP=`ifconfig $BR | grep inet | cut -d B -f1 | sed s/[^0-9.]//g`
AP=`iwconfig $WIFI | grep Access | cut -d" " -f18`
ESSID=`iwconfig $WIFI | grep ESSID | cut -d\" -f2`
RATE=`iwconfig $WIFI | grep Rate | cut -d"M" -f1 | cut -b20-25 | cut -d" " -f1`
UPTIME=`cat /proc/uptime | cut -d"." -f1`
CLI=`cat /proc/net/arp | grep $BR | wc -l | sed s/[^0-9]//g`

WGET="$WGET?IP=$IP&AP=$AP&ESSID=$ESSID&UPTIME=$UPTIME&CLI=$CLI&RATE=$RATE"
echo $WGET

iwlist $WIFI peers | grep 'Quality' | while read line;
do
  MAC=`echo $line | cut -d" " -f1`
  QUAL=`echo $line | cut -d"=" -f2 | cut -d"/" -f1`
  SIG=`echo $line | cut -d"=" -f3 | cut -d" " -f1`
  NOISE=`echo $line | cut -d"=" -f4 | cut -d" " -f1`

  WGET="$WGET&MAC[]=$MAC&QUAL[]=$QUAL&SIG[]=$SIG&NOISE[]=$NOISE"
  echo $WGET
done

echo $WGET
#wget -O /dev/null "$WGET"

If you run the above script, the local WGET within the loop is updated and shows the complete URL with all peers, but the last WGET is not getting updated with the contents within the loop.

What can be done to update the WGET so it contains info from all peers?


BR

Arne-J.
ajauberg is offline   Reply With Quote
Old 4 Weeks Ago   #2 (permalink)
vsemaska
Linux Newbie
 
Join Date: Jun 2007
Posts: 207
The problem is when you pipe data to a loop a child process is created to run the loop. Any variable settings within the child process are not seen in the parent process. The thing to do is direct the output to a file and then use that in the loop.

Code:
iwlist $WIFI peers | grep 'Quality' > /tmp/tmp.tmp
while read line
    do
       .
       .
       .
    done < /tmp/tmp.tmp
rm -f /tmp/tmp.tmp
vsemaska is offline   Reply With Quote
Old 4 Weeks Ago   #3 (permalink)
ajauberg
Just Joined!
 
Join Date: Jul 2008
Posts: 4
Thank you, it worked!

The updated script:

Code:
#!/bin/sh

WIFI=ath0
BR=br0
TMP=/tmp/tmp.tmp

# Update the MAC, IP address and peers of the AP
WGET=http://<mysite>/update.php

IP=`ifconfig $BR | grep inet | cut -d B -f1 | sed s/[^0-9.]//g`
AP=`iwconfig $WIFI | grep Access | cut -d" " -f18`
ESSID=`iwconfig $WIFI | grep ESSID | cut -d\" -f2`
RATE=`iwconfig $WIFI | grep Rate | cut -d"M" -f1 | cut -b20-25 | cut -d" " -f1`
UPTIME=`cat /proc/uptime | cut -d"." -f1`
CLI=`cat /proc/net/arp | grep $BR | wc -l | sed s/[^0-9]//g`

WGET="$WGET?IP=$IP&AP=$AP&ESSID=$ESSID&UPTIME=$UPTIME&CLI=$CLI&RATE=$RATE"
#echo $WGET

rm -f $TMP
iwlist $WIFI peers | grep 'Quality' > $TMP
while read line
do
  MAC=`echo $line | cut -d" " -f1`
  QUAL=`echo $line | cut -d"=" -f2 | cut -d"/" -f1`
  SIG=`echo $line | cut -d"=" -f3 | cut -d" " -f1`
  NOISE=`echo $line | cut -d"=" -f4 | cut -d" " -f1`

  WGET="$WGET&MAC[]=$MAC&QUAL[]=$QUAL&SIG[]=$SIG&NOISE[]=$NOISE"
#  echo $WGET
done < $TMP

echo $WGET
#wget -O /dev/null "$WGET"
ajauberg is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 10:17 AM.




© 2000 - 2008 - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.0.0