#!/bin/sh -u # DAT2330 03W Unix Final - Tuesday Script Sample Solution # $0 [ number ] # -IAN! idallen@ncf.ca # Step 1 - 2 marks - correct header (interpreter, PATH, and umask) # Set a standard search path and non-restrictive umask. # export PATH=/bin:/usr/bin umask 022 # Prompt and read a file name. # Exit the script if the user enters nothing. # Step 2 - 3 marks # echo 1>&2 "Enter a file name:" read path || exit 1 # Step 3 - 6 marks # if [ -z "$path" ] ; then echo 1>&2 "$0: empty input file name" exit 1 fi # Make sure the pathname is not a directory, is not empty, is readable. # Step 4 - 6 marks # if [ -d "$path" ]; then echo 1>&2 "$0: '$path' is a directory (expecting a file)" exit 2 fi # Step 5 - 6 marks # if [ ! -s "$path" ]; then echo 1>&2 "$0: file '$path' is missing or empty (zero size)" exit 3 fi # Step 6 - 6 marks # if [ ! -r "$path" ]; then echo 1>&2 "$0: file '$path' is not readable" exit 4 fi # Step 7 - 3 marks # Count the lines in the file. # lines=$( wc -l <"$path" ) # Make sure that a file named with your Algonquin userid exists. # Append the count of lines in the $path to the $userid file. # Step 8 - 7 marks # userid="$USER" # or just enter your actual abcd0123 userid if [ ! -e "$userid" ]; then echo 1>&2 "$0: Output file '$userid' is missing from current directory" exit 5 fi # Step 9 - 3 marks # echo "Found $lines lines in file '$path'" >>"$userid" # Step 10 - 7 marks # Exit the script if more than one argument is given. # if [ $# -gt 1 ]; then echo 1>&2 "$0: expecting one optional number, found $# ($*)" exit 6 fi # Step 11 - 4 marks # Use the number 1000 if no argument is given. # if [ $# -eq 0 ]; then numlimit=1000 else numlimit="$1" fi # Step 12 - 7 marks # if [ "$numlimit" -gt "$lines" ] ; then echo "Limit number $numlimit is > lines $lines from file '$path'" exit 7 fi