#!/bin/sh -u # fetch via WWW the current temperature for a select set of Canadian cities # ------------------------------------------------------------------ # Syntax: # $0 [ city_name ] # ------------------------------------------------------------------ # Purpose: # Fetch via WWW the current temperature for a select set of Canadian cities. # The city name can be any set of unique characters in the city name. # This only recognizes cities on the main Enviro Canada web page. # ------------------------------------------------------------------ # Student Name: Ian! D. Allen # Algonquin EMail Address: alleni # Student Number: 000-000-000 # Course Number: NET 2003 # Lab Section Number: 010 # Professor Name: Ian Allen # Assignment Name/Number/Date: Exercise 5 # Comment: Sample solution (missing comments) # ------------------------------------------------------------------ # Step 1 (includes all the lines above too) PATH=/bin:/usr/bin ; export PATH umask 022 LC_COLLATE=C ; export LC_COLLATE # Step 2 # date # wc /etc/passwd # Step 3 # echo "$NOSUCHVARIABLE" # Step 4 if [ $# -eq 0 ] ; then echo 1>&2 "Enter city name for weather:" read city || exit 1 elif [ $# -eq 1 ] ; then city="$1" else echo 1>&2 "$0: Expecting one city name argument, found $# ($*)" exit 2 fi # Step 5 if [ "$city" = "" ] ; then echo 1>&2 "$0: City name is empty; nothing done" exit 3 fi echo "Using city name '$city'" # Step 6 weatherURL='http://text.weatheroffice.ec.gc.ca/' # Step 7 mainpage=mainpage.html if [ ! -s "$mainpage" ] ; then wget -q -O "$mainpage" "$weatherURL" status=$? if [ ! -s "$mainpage" ] ; then echo 1>&2 "$0 '$city' status $status: failed to fetch '$weatherURL'" rm "$mainpage" exit 4 fi fi # Step 8 englink=$( grep -B 1 "english" $mainpage | head -1 | awk -F'"' '{print $2}' ) if [ "$englink" = "" ] ; then echo 1>&2 "$0: '$city' Cannot find english hyperlink in '$mainpage'" echo 1>&2 "$0: Renamed file to '$mainpage.bak'" mv "$mainpage" "$mainpage".bak exit 5 fi # echo "DEBUG: Found '$englink'" # Step 9 englishURL="$weatherURL/$englink" englishpage=englishpage.html if [ ! -s "$englishpage" ] ; then wget -q -O "$englishpage" "$englishURL" status=$? if [ ! -s "$englishpage" ] ; then echo 1>&2 "$0 '$city' status $status: failed to fetch '$englishURL'" rm "$englishpage" exit 6 fi fi # Step 10 citylink=$( grep "$city" "$englishpage" \ | grep '/forecast/city' \ | awk -F"'" '{print $2}' ) if [ "$citylink" = "" ] ; then echo 1>&2 "$0: Cannot find '$city' in '$englishpage' from '$englishURL'" exit 7 fi # Step 11 lines=$( echo "$citylink" | wc -l ) if [ $lines -ne 1 ] ; then echo 1>&2 "$0: City '$city' matches $lines forecasts:" echo 1>&2 "$citylink" echo 1>&2 "$0: Please be more specific" exit 8 fi # echo "DEBUG: Found $citylink" # Step 12 cityURL="$weatherURL/$citylink" citypage=citypage.txt lynx -dump "$cityURL" >"$citypage" status=$? # Step 13 if [ ! -s "$citypage" ] ; then echo 1>&2 "$0 lynx status $status: failed to fetch '$city' via '$cityURL'" rm "$citypage" exit 9 fi # Step 14 if grep ERROR "$citypage" ; then echo 1>&2 "$0: error fetching '$city' via '$cityURL'" rm "$citypage" exit 10 fi # Step 15 fullname=$( grep -B 4 'Currently' "$citypage" | head -1 ) # Step 16 temp=$( grep -A 1 Temperature: "$citypage" | tail -1 ) # Step 17 echo "The temperature in $fullname is $temp" | tr -s ' '