CS 2204 Lab 10

Your name here (please print):

Your student ID number here:

  1. (1 point). Go to http://www.weather.com and get local current weather for Blacksburg (zip 24060). In your browser go to "View" and select "page source". Figure out where the temperature information is located. <B CLASS=obsTempTextA>65&deg;F</B>
  2. (1 point) Identify what URL needs to be specified for the above page and write a bash script that fetches it to a file named "page". Follow the stock price example from last class.
    Verify that the page you get is the same as in 1). #!/bin/bash
    wget "http://www.weather.com/weather/local/24060" -o /dev/null -O page
  3. (2 points) Put a check in your script that verifies that at least a single argument is given. If not, the script exits. #!/bin/bash
    if [ $# -eq 0 ]
    then
      echo "No arguments given."
      exit 2
    fi
    wget "http://www.weather.com/weather/local/24060" -o /dev/null -O page
  4. (2 points) Add a check that checks the argument to be a valid US zip code (that it's a number with exactly five digits). You may find HW2 useful. #!/bin/bash
    if [ $# -eq 0 ]
    then
      echo "No arguments given."
      exit 2
    fi
    # Take the zipcode, and see if it matches this regular expression.
    # $? is the return code of the last command, which is the return
    # of egrep, indicating if the zipcode matches our pattern
    echo $1 | egrep "^[0-9]{5}$" >/dev/null
    if [ "$?" -ne "0" ]; then
      echo "$1 isn't a valid zip"
    else
      wget "http://www.weather.com/weather/local/24060" -o /dev/null -O page
    fi
  5. (4 points) Wrap up by making your script output current weather (in degrees F) for any number of valid zip codes given as the arguments. You may find Lab 9, #2 useful.
    The output should look like: "Currently, it is xx degrees F in location yyyyy" (yyyyy -- the entered zip). #!/bin/bash
    if [ $# -eq 0 ]
    then
      echo "No arguments given."
      exit 2
    fi
    for i
    do
      # Take the zipcode, and see if it matches this regular expression.
      # $? is the return code of the last command, which is the return
      # of egrep, indicating if the zipcode matches our pattern
      echo $i | egrep "^[0-9]{5}$" >/dev/null
      if [ "$?" -ne "0" ]; then
        echo "$i isn't a valid zip"
      else
        wget "http://www.weather.com/weather/local/$i" -o /dev/null -O page
        temperature=`grep temp= page | cut -d "&" -f 2 | cut -d = -f 2`
        echo "Currently, it is $temperature degrees F in location $i"
      fi
    done
  6. (bonus 2 points) Make the above script output a more user-friendly line:
    "Currently, it is xx degrees F in Blacksburg, VA" (where the city automatically corresponds to the zip entered ). #!/bin/bash
    if [ $# -eq 0 ]
    then
      echo "No arguments given."
      exit 2
    fi
    for i
    do
      # Take the zipcode, and see if it matches this regular expression.
      # $? is the return code of the last command, which is the return
      # of egrep: "0" means everything worked that is egrep found a match, i.e. zipcode matches our pattern
      echo $i | egrep "^[0-9]{5}$" >/dev/null
      if [ "$?" -ne "0" ]; then
        echo "$i isn't a valid zip"
      else
        wget "http://www.weather.com/weather/local/$i" -o /dev/null -O page
        temperature=`grep temp= page | cut -d "&" -f 2 | cut -d = -f 2`
        location=`grep "<TITLE>" page | awk '{ print $5, $6 }'
        echo "Currently, it is $temperature degrees F in $location"
      fi
    done