#!/bin/sh -u # DAT2330 03W Unix Final - Wednesday 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 fpath='/etc/passwd' else fpath="$1" fi # Make sure the pathname is not empty, is a file, and is readable. # Step 4 - 6 marks # if [ ! -s "$fpath" ]; then echo 1>&2 "$0: pathname '$fpath' is missing or empty" exit 2 fi # Step 5 - 6 marks # if [ ! -f "$fpath" ]; then echo 1>&2 "$0: pathname '$fpath' is not a plain file" exit 3 fi # Step 6 - 6 marks # if [ ! -r "$fpath" ]; then echo 1>&2 "$0: file '$fpath' is not readable" exit 4 fi # Step 7 - 3 marks # Count the characters in the file. # numchar=$( wc -c <"$fpath" ) # Make sure that a file named with your Algonquin userid exists. # Append the count of characters in the $fpath to the $outfile file. # Step 8 - 7 marks outfile="$USER" # or just enter your actual abcd0123 userid if [ ! -e "$outfile" ]; then echo 1>&2 "$0: Output file '$outfile' is missing from current directory" exit 5 fi # Step 9 - 3 marks # echo "Input file '$fpath' contains $numchar characters" >>"$outfile" # Prompt and read a size limit. # Exit the script if the user enters nothing. # Step 10 - 3 marks # echo 1>&2 "Enter a numeric size limit:" read numin || exit 1 # Step 11 - 6 marks # if [ -z "$numin" ] ; then echo 1>&2 "$0: empty input count - nothing done" exit 6 fi # Step 12 - 7 marks # Issue a message if the limit is >= character count of the file. # if [ "$numin" -ge "$numchar" ] ; then echo "Input number $numin is >= $numchar characters in file '$fpath'" exit 7 fi