#!/bin/sh -u # Test #2 sample answer - Script Part II - 61 marks - 13 of 25% #------------------------------------------------------------ # One line summary: analyse URL against a count of lines #------------------------------------------------------------ # Syntax: $0 URL [ count ] #------------------------------------------------------------ # Purpose: not required for test # Assignment Label: not required for test #------------------------------------------------------------- # Step 1 - 8 marks # - mark for #! shell comment line and option # - mark for one-line summary description # - mark for man-page-like syntax line # - marks for path, collate, lang # - marks for correct umask PATH=/bin:/usr/bin ; export PATH LC_COLLATE=C ; export LC_COLLATE LANG=C ; export LANG umask 026 # Step 2 - 9 marks if [ "$#" -lt 1 -o "$#" -gt 2 ]; then echo 1>&2 "$0: expecting URL and optional integer line count," echo 1>&2 " found $# ($*)" exit 1 fi # Step 3 - 1 mark url="$1" # Step 4 - 2 marks tmp=/tmp/url$$ # Step 5 - 9 marks if ! wget -O "$tmp" "$url" ; then echo 1>&2 "$0: wget of '$url' failed" rm "$tmp" exit 2 fi # Step 6 - 8 marks if [ ! -s "$tmp" ] ; then echo 1>&2 "$0: fetched URL '$url' is empty (zero size)" rm "$tmp" exit 3 fi # Step 7 - 3 marks chmod g-r "$tmp" || exit 4 # Step 8 - 5 marks urlcount=$( wc -l <"$tmp" ) || exit 5 # Step 9 - 4 marks if [ $# -eq 2 ]; then count="$2" else count=100 fi # Step 10 - 7 marks if [ $urlcount -gt $count ]; then echo "Your URL '$url' contains more than $count lines: $urlcount." fi if [ $urlcount -lt $count ]; then echo "Your URL '$url' contains less than $count lines: $urlcount." fi # Step 11 - 5 marks if [ $urlcount -ne $count ] ; then rm "$tmp" exit 6 fi exit 0