eriksmartt.com>Selected Archives

A simple shell script for grabbing the current temperature from the command line

In case someone else needs it, here's a simple shell script I use for grabbing the current weather conditions by U.S. zipcode using the Yahoo APIs:

function weather {
  zipcode=$1
  if [ -n "$zipcode" ]; then
    lynx -dump "http://weather.yahooapis.com/forecastrss?p=$zipcode" | grep -i condition | awk -F' ' '{print $4 $5 $6}' | awk -F'< ' '{print $1}' | sed 's/,/ /'
  else
    echo 'USAGE: weather &lt;zipcode&gt;'
  fi
}

The script uses lynx to grab the Yahoo RSS feed, piping the output to grep, which extracts the line containing the word 'condition'. awk then pulls out some specific fields (delimited by spaces, then later by '<') and sed converts the commas to spaces for prettier output. Obviously, the whole thing is fairly fragile, and changes to the RSS format (which happened maybe a month back) break the code. Checking the weather in Austin with the command: weather 78701, currently outputs: "Fair 67F".