Skip to content

Instantly share code, notes, and snippets.

@sophieforceno
Last active December 11, 2021 23:38
Show Gist options
  • Save sophieforceno/85f0985054970edc510fd27b46582e63 to your computer and use it in GitHub Desktop.
Save sophieforceno/85f0985054970edc510fd27b46582e63 to your computer and use it in GitHub Desktop.
Bash script weather forecast via the Weather Underground API
#! /bin/bash
#
# wf.sh - Weather forecasts in your terminal! - v1.0
# by Sophie Forceno
#
# To use this script, you must register for the Weather Underground API:
# https://www.wunderground.com/weather/api/
# Obtain an API key and insert it here:
api_key=""
zipcode="$1"
# Prompt user if zipcode omitted
if [[ -z $zipcode ]]; then
read -p "Enter your zipcode: " zipcode
fi
# Check length of zipcode
zip_num=${#zipcode}
if [[ $zip_num < 5 ]]; then
echo -e "Error: Insufficient digits in zipcode \n"
exit 0
fi
echo -n "Weather forecast for "
date "+%a %b %d"
curl -s http://api.wunderground.com/api/$api_key/forecast/geolookup/conditions/q/$zipcode.json | grep -w 'title\|fcttext' | cut -d ':' -f2 | sed -n '1!p' | sed -e 's/\"//g' -e 's/,//g' -e '1~2s/$/:/g'
# This API method is deprecated, and it requires perl, but you are welcomed to use it
# It provides a more verbose forecast
# curl -s "http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query=${@:-$ZIP}"|perl -ne '/<title>([^<]+)/&&printf "%s: ",$1;/<fcttext>([^<]+)/&&print $1,"\n"';
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment