Google Site SearchFN Site Search FN Blog Login FN Blog Login
Site Navigation:
 
 

FUN WITH SHELLS: a nifty command line driven weather forecast

by Andrew Newman (CREATED: 2004-02-24)

This is the first, in what I plan to be a series, called "Fun with Shells". The point of these articles will be to not only pass around some increadibly useful simple shells, but also, hopefully encourage people to jump in and either modify these or write their own.

This contribution is a very simple bash shell that I wrote for calling up the weather forecast (thanks to Wunderground.com. What is nice about this shell and the wunderground site, is that you can call up the forecast for just about anywhere (at least from what I can tell).


Here is the script
#!/bin/bash 
# program myforecast
# version 1.0  
# written by Andrew V. Newman
# last modified Tue Feb 24 17:21:57 MST 2004

# shell script to bring up a weather forecast either on a
# graphical browser or in the console if the screen is not available.

# Create a default location.  This assumes you live in the US as I do.
  CITY=Los_Alamos  # use underscore to separate multi-word cities
  STATE=NM 
# Sets a default URL BASE  
  URL_SUF=US/$STATE/$CITY.html 
# Set the default graphical browser 
# I like galeon, but it may not be installed on your machine.
  GUI_BROWSER=/usr/bin/galeon
  GUI_BROWSER=/usr/bin/konqueror
  GUI_BROWSER=/usr/bin/mozilla

# ---------------------------- #
# Shouldn't have to change these
  LYNX=/usr/bin/lynx
  URL_BASE=http://www.wunderground.com/
  MBL_URL_BASE=http://mobile.wunderground.com/
# If you use just one variable
  if [ $1 ] ; then
     URL_SUF=cgi-bin/findweather/getForecast?query=$1
  fi
# If you use two variables
  if [ $2 ]; then
     CITY=$1 
     STATE=$2

     URL_SUF=US/$STATE/$CITY.html
  fi
# If you can display a window on your terminal it will
# bring up your forecast in a graphical browser
  if [ $DISPLAY ]; then
     URL=${URL_BASE}${URL_SUF}
     $GUI_BROWSER $URL 2> /dev/null
# Otherwise, it will bring it up on screen using a version
# of the website set up for mobile computers (no ads and few graphics,
# which will not be displayed anyway).
  else
     URL=${MBL_URL_BASE}${URL_SUF}
     $LYNX $URL -dump 2> /dev/null >forecast.temp
     # displays forecast as text
       more forecast.temp
     # remove temporary file
       rm forecast.temp
  fi
# exit cleanly
  exit 0

It may be easier to just download a copy (right click).

Once you have a copy on your system, all you need to do is:

  • Modify the first two variables to create your own defaults.
  • Change permissions of the file so that you can execute it:
    % chmod 755 myforecast
  • Move file to a directory in your path:
    I would suggest creating a bin directory from your home for all shells and then making certain it is included in your path. % mkdir ~/bin
    % mv myforecast ~/bin/
    If it is not already there, add the following line to ~/.cshrc if you are using a csh or tcsh shell path = ( $path ~/bin) Or, if you are using bash add the following line to ~/.bashrc PATH=$PATH:~/bin

If you look through the program, you will notice that there is a default set up for my home town. Certainly you should change to whatever you want. It is important to note here, that though wunderground.com gives forecasts for cities around the globe, the script is really set to work within the US.
Ok, now that you have set your defaults, give the program a shot. Try running the following commands: % myforecast
% myforecast Chicago IL
% myforecast 87544
% myforecast Tokyo

UPDATED: Check out 2004-03-02 MYRADAR: a radar companion to myforecast.

Please email me if you have any comments or suggestions!