#!/bin/sh -u # DAT2330 03W Unix Final - Monday Script Sample Solution # Syntax: $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 filename, found $# ($*)" exit 1 fi # Step 3 - 4 marks # Use the Unix password file if no arguments are given. # if [ $# -eq 0 ]; then file='/etc/passwd' else file="$1" fi # Make sure the pathname is a file, readable, and not empty. # Step 4 - 6 marks # if [ ! -f "$file" ]; then echo 1>&2 "$0: '$file' is missing, inaccessible, or not a plain file" exit 2 fi # Step 5 - 6 marks # if [ ! -r "$file" ]; then echo 1>&2 "$0: file '$file' is not readable" exit 3 fi # Step 6 - 6 marks # if [ ! -s "$file" ]; then echo 1>&2 "$0: file '$file' is empty (zero size)" exit 4 fi # Step 7 - 3 marks # Count the characters in the file. # count=$( wc -c <"$file" ) # 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 incount || exit 1 # Step 9 - 6 marks # if [ -z "$incount" ] ; 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 <= the limit. # if [ "$count" -le "$incount" ] ; then echo "File '$file' size $count is <= $incount" exit 6 fi # Make sure that a file named with your Algonquin userid exists. # Append the count of characters in the $file to the $out file. # Step 11 - 7 marks # out="$USER" # or just enter your actual abcd0123 userid if [ ! -e "$out" ]; then echo 1>&2 "$0: Output file '$out' is missing from current directory" exit 7 fi # Step 12 - 3 marks # echo "File '$file' contains $count characters" >>"$out"