#!/bin/sh -u # Answer file: Exercise #3 - myweather.sh # show the current temperature/pressure/visibility/humidity for a city #------------------------------------------------------------------ # Syntax: $0 [ city_code [ weather_item ] ] #------------------------------------------------------------------ # Purpose: # Display the current temperature/pressure/visibility/humidity for a # given city. Print the result in a nice, readable format. # Prompt for any missing arguments if standard input is a terminal. # If not a terminal, a missing city code defaults to "YOW". # If not a terminal, a missing weather item defaults to "temperature". # Note: This script is not intended to be used as a CGI script; # since, it outputs no CGI header and errors print to standard error. #------------------------------------------------------------------ # Student Name: Ian Allen # Algonquin EMail Address: alleni # Student Number: 074-210-779 # Course Number: DAT2330 # Lab Section Number: 010 # Professor Name: Ian Allen # Assignment Name/Number/Date: Exercise #3 due November 23 # Comment: This is a sample solution. #------------------------------------------------------------------ # Set a standard shell search path and a friendly umask. # PATH=/bin:/usr/bin ; export PATH umask 022 #------------------------------------------------------------- # Input section. # Exit the script if more than two arguments are supplied. # if [ $# -gt 2 ]; then echo 1>&2 "$0: Expecting city code and weather item; found $# args ($*)" exit 1 fi # The user might have entered 0, 1, or 2 arguments. # Prompt for missing arguments if standard input is a terminal. # Use defaults for missing arguments if no terminal. # if [ $# -eq 0 ]; then if tty -s ; then echo 1>&2 "Enter city code and weather item:" read citycode weatheritem || exit $? else citycode=YOW weatheritem=temperature echo "Using default city code $citycode and weather item $weatheritem" fi elif [ $# -eq 1 ]; then citycode="$1" # first arg is citycode if tty -s ; then echo 1>&2 "Enter weatheritem:" read weatheritem || exit $? else weatheritem=temperature echo "Using default weather item $weatheritem" fi else # we must have exactly two arguments on the command line # citycode="$1" # first arg is citycode weatheritem="$2" # second arg is weatheritem fi # #------------------------------------------------------------- #------------------------------------------------------------- # Input validation section. # Count the characters in the city code; make sure there are three. # Subtract one to account for the newline at the end of the line. # count=$( echo "$citycode" | wc -c ) let count=count-1 if [ "$count" -ne 3 ] ; then echo 1>&2 "$0: Expecting 3-character city code, found $count '$citycode'" exit 2 fi # Check for one of the four weather items we know how to fetch. # Choose a search pattern based on the item. # Exit if the item is not one of the four we accept. # if [ "$weatheritem" = "temperature" ] ; then pattern="Temp.:" elif [ "$weatheritem" = "pressure" ] ; then pattern="Pressure:" elif [ "$weatheritem" = "visibility" ] ; then pattern="Visibility:" elif [ "$weatheritem" = "humidity" ] ; then pattern="Humidity:" else echo 1>&2 "$0: Unknown weather item '$weatheritem'" echo 1>&2 "$0: Expecting temperature, pressure, visibility, or humidity" exit 3 fi # #------------------------------------------------------------- #------------------------------------------------------------- # Processing section. # # Make the city code UPPER CASE. # citycode=$( echo "$citycode" | tr 'a-z' 'A-Z' ) # Fetch the page for the city code. # page=$( lynx -dump "http://weatheroffice.ec.gc.ca/forecast/city_e.html?$citycode" ) # Look for the string "Error" in the fetched page and exit if found. # if echo "$page" | grep "Error" >/dev/null ; then echo 1>&2 "$0: Cannot find weather for city code '$citycode'" exit 4 fi # Extract the weather item and the city name. # Note: the fields may have many extra blanks around them still. # Note: looking for ": Issued" fails for some weather pages. # value=$( echo "$page" | grep -e "$pattern" | awk -F: '{print $2}' ) cityname=$( echo "$page" | grep -e "Issued" | awk -F: '{print $1}' ) # #------------------------------------------------------------- #------------------------------------------------------------- # Output section. # # Generate nice output with multiple blanks squeezed down to one. # echo "The $weatheritem in $citycode - $cityname - is $value" | tr -s ' ' # script exits with the return code of the last command executed