LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-22-2014, 05:21 PM   #1
andrew44
Member
 
Registered: Apr 2011
Posts: 31

Rep: Reputation: 0
script to change vpn connection if slow vpn connection


I want to write a script to pick one of 2 vpn connections, I might add more but I need to get over my first hurdle which is why I'm pasting here.

As long as the connection is above 100 as calculated by the wget statement in line 3 all is good.

But if it drops to below 100 I get a message

./vpngood.sh: line 6: [: 81.2: integer expression expected

-------------------------------------------------------

#!/bin/bash
#My name is vpngood.sh
#set -x
AVERAGE=`wget -O /dev/null http://somewebsite.jpg 2>&1 | grep '\([0-9.]\+ [KM]B>
if [ $AVERAGE -gt 100 ]
then
echo "Average is above 100"
else
echo "Count is below than 100"
fi

-------------------------------------------------------
If I manually set Average to a number it works fine.

What am I doing wrong?

Thanks
 
Old 02-23-2014, 09:05 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Can you copy and paste the exact script? This line cannot be correct ...
Code:
AVERAGE=`wget -O /dev/null http://somewebsite.jpg 2>&1 | grep '\([0-9.]\+ [KM]B>
... because there is no closing ' and no closing `

When posting it, please use CODE tags (most easily done by posting in Advanced Mode and using the # button) to preserve indentation and let the code line up nicely.

What is the value of AVERAGE when it fails? Apparently it is not an integer so you could test whether it is an integer before testing its value:
Code:
if [[ $INTEGER =~ ^[0-9]+$ ]]; then
    if ((AVERAGE > 100 )); then
        echo "Average is above 100: $AVERAGE"
    else
        echo "Average is below or equal to 100: $AVERAGE"
    fi
else
    echo "Average is not a number: $AVERAGE"
fi
I have taken the liberty of using [[ ... ]] instead of [ ... ] because it is all round a better test for reasons explained here and (( ... )) instead of [ ... ] for the numeric test because the syntax is cleaner. You might also like to start using $( ... ) instead of ` ... ` for reasons explained here.
 
Old 02-23-2014, 11:25 AM   #3
andrew44
Member
 
Registered: Apr 2011
Posts: 31

Original Poster
Rep: Reputation: 0
This is the script as it is at the moment. It works now, thanks for the pointers.

Code:
#!/bin/bash
#My name is vpngood.sh
#set -x
AVERAGE=$(wget -O /dev/null http://i.dailymail.co.uk/i/pix/2013/07/04/article-2355815-18AFDE11000005DC-143_306x955.jpg 2>&1 \
| grep '\([0-9.]\+ [KM]B/s\)'| awk '{print $3}'|sed 's/[!@#\$%^&*()]//g')
    if ((AVERAGE > 140 ));
    then
        echo "Average is greater than 140, its $AVERAGE"
else
        echo "Average is below 140, it's $AVERAGE ."
fi
 
  


Reply

Tags
bash script $@, scripting



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
VPN Connection - Not able to browse other sites when connecting to VPN arindom Linux - Networking 25 07-02-2012 01:43 PM
Routing change by pptp VPN connection. agkbill Linux - Networking 2 06-29-2011 02:27 AM
[SOLVED] network-manager, vpn: no/slow connection, options not saved (debian squeeze) mike11 Linux - Networking 2 01-23-2010 09:43 PM
Slow VPN connection? frankkky General 1 01-07-2009 01:46 PM
VPN - is there a way to use VPN & normal connection at the same time? natv Linux - Networking 4 09-05-2007 08:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:48 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration