Friday, October 3, 2008

Bash Weather Script - World Update!

Hey There,

We're back from yesterday's insanity (you called my bluff and I hope my response was satisfactory ;) and, yeah, I wasted my entire evening watching the Vice Presidential debate and then making the mistake of discussing it with people who didn't agree with me. Politics and Religion... When am I going to learn? ;)

Today's Linux/Unix bash shell script is an update on our Bash Weather Script. the info is still grabbed from WeatherBug.com. Check out our older bash script posts on spewing out famous quotations on pretty much any subject, doing encylopedia lookups, accessing the online Thesaurus, translating between different languages and, of course, using the online dictionary.

This script is fairly easy to run and but takes a few more arguments now. You can run it like this:

host # ./weather.sh 65873

or

host # ./weather.sh Berlin Germany <-- capitalization doesn't count ;)

Below are a few screen shots of the script's new-fangled output (it's actually just "more of the same"... oh no, I'm channeling campaign slogans now ;).

Click on either picture and watch it grow into something you can actually read ;)

New Local Weather Output

New World Weather Output

Hope you enjoy this script, and can find some use for it. Any suggestions for improvement would be greatly appreciated.

Enjoy your weekend and learn from me: Don't argue politics with your wife. The couch is an uncomfortable bed ;)

Cheers,


Creative Commons License


This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

#!/bin/bash

#
# weather.sh - It's raining somewhere in the world :)
#
# 2008 - Mike Golvach - eggi@comcast.net
#
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License
#

numargs=$#
arg1=$1
arg2=$2

if [ $numargs -lt 1 ]
then
echo "Usage: $0 [YourZipCode|Town Country]"
echo "Ex: $0 60015"
echo "Ex: $0 Berlin Germany"
exit 1
fi

if [ $numargs -eq 2 ]
then
args=`echo $args|sed 's/ /\+/g'`
elif [ $# -gt 2 ]
then
echo "Usage: $0 [YourZipCode|Town Country]"
echo "Ex: $0 60015"
echo "Ex: $0 Berlin Germany"
exit 1
fi

echo

args="$@"
wget=/usr/bin/wget


echo -n "Your Forecast:"

if [ $numargs -eq 1 ]
then
$wget -nv -O - "http://weather.weatherbug.com/Common/SearchResults.html?is_search=true&nav_section=1&loc_country=WORLD&zcode=z6286&loc=$args" 2>&1|grep -w For|grep h3|grep $args|sed -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/$/\n/'

$wget -nv -O - "http://weather.weatherbug.com/Common/SearchResults.html?is_search=true&nav_section=1&loc_country=WORLD&zcode=z6286&loc=$args" rel="nofollow" 2>&1|sed -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/$/\n/'|sed -e '1,/Your Forecast/d' -e '/7-Day Forecast/,$d'|sed -e '/^[ \t]*$/d' -e '/^$/N;/\n$/D' -e 's/[ \t]*more/... more\n/'|sed 's/°/Degrees/g'
else
echo " For $arg1, $arg2"
$wget -nv -O - "http://weather.weatherbug.com/$2/$1-weather.html?nav_section=1&zcode=z6286" 2>&1|sed -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/$/\n/'|sed -e "1,/Your Forecast for $arg1, $arg2/d" -e '/7-Day Forecast/,$d'|sed -e '/^[ \t]*$/d' -e '/^$/N;/\n$/D' -e 's/[ \t]*more/... more\n/'|sed 's/°/Degrees/g'
fi

exit 0


, Mike




Please note that this blog accepts comments via email only. See our Mission And Policy Statement for further details.