Skip to content

Instantly share code, notes, and snippets.

@kathawala
Created November 26, 2014 09:15
Show Gist options
  • Save kathawala/384a4bf3664a4dccebac to your computer and use it in GitHub Desktop.
Save kathawala/384a4bf3664a4dccebac to your computer and use it in GitHub Desktop.
Script to fetch weather data from API
#!/bin/bash
API=
# Set URL for fetching weather information and an Error Msg for invalid input
URL=http://api.wunderground.com/api/$API/geolookup/conditions/q/CA/Stanford.xml
ERR="Error: Arguments expected in form [City] [State]. Ex: weather Palo_Alto CA"
# handles arguments for location / default is Stanford CA
# well-formed argument example: "weather Memphis TN"
if [ ! -z "$1" ] && [ ! -z "$2" ]
then
URL=${URL/Stanford/$1}
URL=${URL/CA/$2}
fi
# Handles erroneous input such as:
# no second argument or a second argument with invalid State Code
# bad argument example: "weather Memphis T"
if [[ ! -z "$1" && -z "$2" ]] || [[ ! -z "$2" && ! ${#2} -eq 2 ]]
then
echo "$ERR"
exit 1
fi
# get xml file from API, parse out relevant information and echo to stdout
# NOTE: ~/bin/w.xml~ MUST BE AN EXISTENT FILE!
curl -s $URL > ~/bin/w.xml~
if grep -qom1 "<icon_url>" ~/bin/w.xml~; then
cat ~/bin/w.xml~ > ~/bin/w.xml
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment