gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Increment environmental variables in bash script

Sat Jan 05, 2013 12:08 pm

Hello,

I'm trying to write a bash script that (once added to cronjobs) will do the following:

1. Checks if wi-fi is up by doing a ping to google.com => if ok exit. If not increment a env. variable.
2. (at second run) if env. variable is 1 (e.g. the ping test failed at previous run) reset the usb interface (with an external script that works fine) and increment the env. variable again (set it to 2) and exit
3. (at third run) if env. variable is 2 then reset the network interface again (otherwise wi-fi will still not work) and set env. variable to 0 (back to step 1)

I realize that this might not be the optimum way, but I'm very new to bash scripting. So what I did so far:

Code: Select all

#!/bin/bash

if [ -n $TESTVAR ]
then
 if [ "$TESTVAR" == "0" ]
  then
      ping -c 3 www.google.com > /dev/null 2>&1
     date '+%Y-%m-%d %H:%M:%S all ok' >> /var/log/checkconnection.log
   if [ $? -ne 0 ] ; then
    date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /var/log/checkconnection.log
    /etc/init.d/networking restart
  export TESTVAR=$((TESTVAR+1))
   fi
 exit
 fi
 if [ "$TESTVAR" == "1" ]
  then
    ./usbreset /dev/bus/usb/001/005
    export TESTVAR==$((TESTVAR+1))
    date '+%Y-%m-%d %H:%M:%S USB reset' >> /var/log/checkconnection.log
  
 exit
 fi
 if [ "$TESTVAR" == "2" ]
    then
    ping -c 3 www.google.com > /dev/null 2>&1
     if [ $? -ne 0 ] ; then
     date '+%Y-%m-%d %H:%M:%S Connection Unavailable after usb reset' >> /var/log/checkconnection.log
     /etc/init.d/networking restart
     export TESTVAR=0
     fi
  
 exit
 fi
fi

My first problem is that the line:

export TESTVAR=$((TESTVAR+1))

does not seem to increment the env. variable
I managed to make it execute, and I even checked with echo that indeed it does:
export TESTVAR=1
but still:
root@raspberrypi:/home/pi# echo "$TESTVAR"
0

From what I read about variables they are not always set for the same "environments" (not sure how else to say it), but I'm thinking there must be a way to have true global variables that can be set and read from anywere.

User avatar
pluggy
Posts: 3635
Joined: Thu May 31, 2012 3:52 pm
Location: Barnoldswick, Lancashire,UK
Contact: Website

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 12:21 pm

assuming solwa is your variable

Code: Select all

solwa=$[solwa +1]
will increment it.

The variable remains valid for the bash session. Piping starts a new session so variables don't survive piping.
Don't judge Linux by the Pi.......
I must not tread on too many sacred cows......

gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 3:14 pm

Thanks for the answer.

I understand that a standard variable will not survive after the script is stopped, but I would have expected that a global env. variable would. At least this is what I understood from this post: http://stackoverflow.com/questions/1464 ... ell-script

DirkS
Posts: 10363
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 3:27 pm

gabello wrote:I understand that a standard variable will not survive after the script is stopped, but I would have expected that a global env. variable would. At least this is what I understood from this post: http://stackoverflow.com/questions/1464 ... ell-script
Did you run your script in the current shell by using 'source myscript.sh' or '. myscript.sh´ (dot + space + name of the script) as the post you're linking to mentions?

Gr.
Dirk.

Joe Schmoe
Posts: 4277
Joined: Sun Jan 15, 2012 1:11 pm

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 3:31 pm

gabello wrote:Thanks for the answer.

I understand that a standard variable will not survive after the script is stopped, but I would have expected that a global env. variable would. At least this is what I understood from this post: http://stackoverflow.com/questions/1464 ... ell-script
The short answer is "No.".

Think of it as being like a genetic disease. You can pass it down to your kids (and to your grandkids, and etc.), but you can't pass it to your parents.

The often-given-workaround is to "source" the script rather than run it. This has plusses and minusses - and should be viewed as that - a kludge/workaround, not general practice.
And some folks need to stop being fanboys and see the forest behind the trees.

(One of the best lines I've seen on this board lately)

User avatar
jojopi
Posts: 3270
Joined: Tue Oct 11, 2011 8:38 pm

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 3:43 pm

pluggy wrote:solwa=$[solwa +1]
That is a non-standard syntax, and furthermore man bash says:
The old format $[expression] is deprecated and will be removed in
upcoming versions of bash.
The correct form is $((expression)) as the OP was already using.

gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 3:49 pm

welI tried it with source or . script.sh, but without luck. Looking more into this subject I think I would go a different way and actually write the variable in a standard file and read it from there.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 4:04 pm

Why not use files instead of an env variable, eg /somewhere/failed and /somewhere/failed_again. Your script can test if they exist and create and delete them as necessary.
>)))'><'(((<

gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 4:37 pm

I thought about creating files as you say, but since I also use this a a scripting exercise I think that actually writing a value in a file (depending on the case) and reading the value from there is more... challenging :).

I actually managed to do my script, now I just hope that I will have to reset my PI even less often due to USB issues.

User avatar
aTao
Posts: 1093
Joined: Wed Dec 12, 2012 10:41 am
Location: Howlin Eigg

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 4:52 pm

gabello wrote:I thought about creating files as you say, but since I also use this a a scripting exercise I think that actually writing a value in a file (depending on the case) and reading the value from there is more... challenging :).

I actually managed to do my script, now I just hope that I will have to reset my PI even less often due to USB issues.
Good that you have it working, but I said test if a file exists, not read its contents.
>)))'><'(((<

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 5:19 pm

gabello wrote:I thought about creating files as you say, but since I also use this a a scripting exercise I think that actually writing a value in a file (depending on the case) and reading the value from there is more... challenging :).

I actually managed to do my script, now I just hope that I will have to reset my PI even less often due to USB issues.
Can you post it for all to see ?
Thanks.
Keith
PS, cos I might use it too :)
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

gabello
Posts: 51
Joined: Sun Aug 05, 2012 8:02 pm

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 6:43 pm

Well I'm sure that the script has a lot of issues (as I said I'm a total noob to scripting/programming), and I will see how it behaves a true wi-fi drops, but here it is:

Code: Select all

#!/bin/bash
cat var.txt | while read TESTVAR; do

if [ -n $TESTVAR ]
then

 if [ "$TESTVAR" == "0" ]
  then
   ping -c 3 www.google.com > /dev/null 2>&1
   #date '+%Y-%m-%d %H:%M:%S test' >> /var/log/checkconnection.log
   if [ $? -ne 0 ] ; then
   date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /var/log/checkconnection.log
   /etc/init.d/networking restart
   TESTVAR=$((TESTVAR+1))
   echo "$TESTVAR" > var.txt 
   fi
 exit
 fi
 if [ "$TESTVAR" == "1" ]
  then
   ping -c 3 www.google.com > /dev/null 2>&1
   #date '+%Y-%m-%d %H:%M:%S test' >> /var/log/checkconnection.log
   if [ $? -ne 0 ] ; then
    ./usbreset /dev/bus/usb/001/005
    TESTVAR==$((TESTVAR+1))
    echo  "$TESTVAR" > var.txt
    date '+%Y-%m-%d %H:%M:%S USB reset' >> /var/log/checkconnection.log
   fi
   date '+%Y-%m-%d %H:%M:%S connection work after net restart' >> /var/log/checkconnection.log
   TESTVAR=0
   echo "$TESTVAR" > var.txt 
 exit
 fi
 if [ "$TESTVAR" == "2" ]
    then
    ping -c 3 www.google.com > /dev/null 2>&1
     if [ $? -ne 0 ] ; then
     date '+%Y-%m-%d %H:%M:%S Connection Unavailable after usb reset try net restart' >> /var/log/checkconnection.log
     /etc/init.d/networking restart
     TESTVAR=0
     echo $TESTVAR > var.txt
     fi
     date '+%Y-%m-%d %H:%M:%S Connection works after usb  reset' >> /var/log/checkconnection.log
     TESTVAR=0
     echo $TESTVAR > var.txt
 exit
 fi
fi

done
The usbreset script is from here: http://linux.mjnet.eu/post/626/how-to-r ... mand-line/


Note that the include statements on that page are wrong, so the correct ones would be:

Code: Select all

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/usbdevice_fs.h>

1. You'll also have to adjust:

./usbreset /dev/bus/usb/001/005


to fit the correct usb device that you want to restart (as explained on the usbreset page above)

2. As you can see I'm writing the values in a file called var.txt in the same folder as the main script, which you will need to create and put 0 as it's only content,


To be honest I've fixed a lot of drop wi-fi issues with a much more simple script that I've used as a cronjob every 15 minutes:

Code: Select all

#!/bin/bash
ping -c 3 www.google.com > /dev/null 2>&1

if [ $? -ne 0 ] ; then
 date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /var/log/checkconnection.log
 /etc/init.d/networking restart
fi

And the present script was more a try to play with scripting language and see if I can reduce the wi-fi drops incidence further.

efflandt
Posts: 359
Joined: Mon Dec 03, 2012 2:47 am
Location: Elgin, IL USA

Re: Increment environmental variables in bash script

Sat Jan 05, 2013 7:40 pm

Why not just ping your router LAN IP? Google may decide it does not like you pinging them all the time and block you (or internet or routing delays could result in false negative). Response to ping is NOT required for networking, it is just a test tool "if" it is enabled. I think Windows firewall blocks ping (ICMP) by default.

Ethernet and USB going down after a random period of time is a known (or at least common) issue for even the most recent December Raspbian version.

However, I have not had ethernet, USB, or mini-WiFi go down in Raspbian since doing rpi-update (other than WiFi going down momentarily only once and automatically coming right back up in earlier rpi-update). So rpi-update for newer firmware might be something to try if you have not: https://github.com/Hexxeh/rpi-update

Although, that may not fix USB hardware that the Pi will not even properly boot with or lacks support.

jamiesk
Posts: 95
Joined: Mon Nov 26, 2012 8:48 pm
Location: Ipswich, Suffolk, England, UK.

Re: Increment environmental variables in bash script

Sun Jan 06, 2013 1:58 pm

gabello wrote:Well I'm sure that the script has a lot of issues (as I said I'm a total noob to scripting/programming), and I will see how it behaves a true wi-fi drops, but here it is:

Code: Select all

#!/bin/bash
ping -c 3 www.google.com > /dev/null 2>&1

if [ $? -ne 0 ] ; then
 date '+%Y-%m-%d %H:%M:%S Connection Unavailable' >> /var/log/checkconnection.log
 /etc/init.d/networking restart
fi

And the present script was more a try to play with scripting language and see if I can reduce the wi-fi drops incidence further.
Thanks for that gabello. I have put this into my solar script, as I had a network drop today :( Here is the addition to my script.

Code: Select all

# Check the network
ping -c 3 192.168.0.254 > /dev/null 2>&1
if [ $? -ne 0 ] ; then
   echo "Network down: restarting"  | logger -t smatool -p local5.info
   /etc/init.d/networking restart
   mail -s "Network Down" keith@email.co.uk < /home/pi/email
fi
I have put the default gateway in, instead of google, thanks efflandt.

Also I use the system logs to record and issues, and then email out too. Obviously once the network has been restarted.

Cheers, Keith :)
Pi1 (Nov 2012 loft)= 1KW immersion controller for Solar panel
Pi2 (Jan 2013 living room)=Play thing
Pi3 (Feb 2013 mobile)= Play thing with Tandy Ladder board,breakout board,Nokia display
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=26&t=28193

Return to “Beginners”