Monday, September 29, 2008

Bash Script To Get Weather Forecasts For Your Zip Code

Hey There,

Today's Linux/Unix bash shell script is probably not the last follow up to the growing stable of scripts we've written to mine the knowledge on tap online. Today's info is grabbed from If you missed any of the others scripts we've jammed out, you can still find them in our older bash script posts to spew out famous quotations on pretty much any subject, do encylopedia lookups, access the online Thesaurus, translate between different languages and, of course, the use the online dictionary. This time we're going to take a crack at getting online weather updates, using zip codes. If you check out the GET variables, you'll notice that the scope is the WORLD, but it doesn't seem to work well with foreign identifiers. Of course, I haven't tried all that hard to get it to work - setting me up for a post on the upgrade ;)

This script is fairly easy to run and somewhat limited. The basic limitations are that it will only work with 5 digit zip codes (no "plus 4") and it will only return a summary weather forecast. If there's more to report, it will indicate this, by letting you know there's ...more information available. You can run it like this:

host # ./ 45432

Like all of our previous scripts, this script uses wget and sed to get the job done. As per usual, this script is a work in progress and could use some spring cleaning, but, hopefully, it's good enough for you to enjoy :)

Below are a few screen shots of the script's output.

Click on either picture to see it as the artist intended it to be seen before the producers ruined everything ;)

Your Personalized Forecast

When Weather Goes Wrong

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

By the way, the forecast for today is cloudy with a chance of all Hell breaking loose in my neighborhood. Here's to your forecast coming back a little more sunny :)


Creative Commons License

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


# - Now you can be wrong 50 percent of the time, too :)
# 2008 - Mike Golvach -
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

if [ $# -lt 1 ]
echo "Usage: $0 Your Zip Code"
echo "Ex: $0 60015"
exit 1


if [ $# -gt 1 ]
echo "Usage: $0 Your Zip Code"
echo "Ex: $0 60015"
exit 1

$wget -nv -O - "$args" 2>&1|grep -i "No matches found for your request" >/dev/null 2>&1


if [ $anygood -eq 0 ]
args=`echo $args|sed 's/%20/\+/g'`
echo "No results found for $args"
exit 2

echo -n "Your Forecast:"

$wget -nv -O - "$args" 2>&1|grep -w For|grep h3|grep $args|sed -e :a -e 's/<[^>]*>/ /g;/</N;//ba' -e 's/$/\n/'

$wget -nv -O - "$args" 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'

exit 0

, Mike

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