#!/bin/sh -u # $0 file [dir] # Run a difference between the passwd file and its sorted self. # Place the output in the file that is the first (mandatory) argument # under the directory that is the second (optional) argument. # Prompt for a missing directory name. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 # 6 marks # Validate the number of arguments; issue a good error message. # if [ $# -eq 0 ]; then echo 1>&2 "$0: Expecting 1 or 2 arguments; found $# ($*)" exit 1 fi # 2 marks # Transfer the argument to a named variable for better readability. # filnam="$1" # 7 marks # Prompt for a missing directory argument. # Put the directory name into a named variable. # if [ $# -eq 1 ]; then echo 1>&2 "$0: Please enter a directory name:" read dirnam else dirnam="$2" fi # 3 marks echo "You are processing file '$filnam' and directory '$dirnam'." # Verify that the input is a writable, searchable directory. # # 5 marks if [ ! -d "$dirnam" ]; then echo 1>&2 "$0: '$dirnam' is not a directory" exit 1 fi # 5 marks if [ ! -w "$dirnam" ]; then echo 1>&2 "$0: '$dirnam' is not a writable directory" exit 1 fi # 5 marks if [ ! -x "$dirnam" ]; then echo 1>&2 "$0: '$dirnam' is not a searchable directory" exit 1 fi # 2 marks # Build the output file name in another variable. # Make sure it is either nonexistent or is zero size. # diffout="$dirnam/$filnam" # 5 marks if [ -s "$diffout" ]; then echo 1>&2 "$0: '$diffout' is not an empty file" exit 1 fi # 3 marks # Construct a temp file name using the process ID, under the tmp # directory. Sort the password file into the temp file. # tfile=/usr/tmp/mytmp$$ # 4 marks sort /etc/passwd >"$tfile" || exit $? # 3 marks # Run diff against the sorted copy; save the results. # Record the exit status of diff. # diff /etc/passwd "$tfile" >"$diffout" # 2 marks dstat="$?" # 6 marks # Append the output status to a non-empty results file. # if [ -s "$diffout" ] ; then echo "Difference exit status was $dstat." >>"$diffout" || exit $? fi