#!/bin/sh -u # DAT2330 03W Unix Final - Thursday Script Sample Solution # $0 [ file ] # -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 # Step 2 - 7 marks # Exit the script if more than one argument is given. # if [ $# -gt 1 ]; then echo 1>&2 "$0: expecting one optional pathname, found $# ($*)" exit 1 fi # Step 3 - 4 marks # Use the Unix password file if no arguments are given. # if [ $# -eq 0 ]; then argfname='/etc/passwd' else argfname="$1" fi # Make sure the pathname is not a directory, is not empty, is readable. # Step 4 - 6 marks # if [ -d "$argfname" ]; then echo 1>&2 "$0: pathname '$argfname' is a directory (want a file)" exit 2 fi # Step 5 - 6 marks # if [ ! -s "$argfname" ]; then echo 1>&2 "$0: file '$argfname' is missing or empty" exit 3 fi # Step 6 - 6 marks # if [ ! -r "$argfname" ]; then echo 1>&2 "$0: file '$argfname' is not readable" exit 4 fi # Step 7 - 3 marks # Count the characters in the file. # bytecnt=$( wc -c <"$argfname" ) # Prompt and read a size limit. # Exit the script if the user enters nothing. # Step 8 - 3 marks # echo 1>&2 "Enter a numeric size limit:" read mynum || exit 1 # Step 9 - 6 marks # if [ -z "$mynum" ] ; then echo 1>&2 "$0: empty input count - nothing done" exit 5 fi # Step 10 - 7 marks # Issue a message if the character count of the file is <= size limit. # if [ "$bytecnt" -le "$mynum" ] ; then echo "Pathname '$argfname' character count $bytecnt is <= $mynum" exit 6 fi # Make sure that a file named with your Algonquin userid exists. # Append the count of characters in the $argfname to the $fout file. # Step 11 - 7 marks fout="$USER" # or just enter your actual abcd0123 userid if [ ! -e "$fout" ]; then echo 1>&2 "$0: Output file '$fout' is missing from current directory" exit 7 fi # Step 12 - 3 marks # echo "I counted $bytecnt characters in file '$argfname'" >>"$fout"