#!/bin/sh -u # $0 dir file # Create a file containing the date and a sorted list of users. # Place the output in the file that is the second argument # under the directory that is the first argument. # -IAN! idallen@ncf.ca export PATH=/bin:/usr/bin umask 022 # 6 marks # Validate the number of arguments; issue a good error message. # if [ $# -ne 2 ]; then echo 1>&2 "$0: Expecting 2 arguments; found $# ($*)" exit 1 fi # Transfer the arguments to named variables for better readability. # 2 marks mydir="$1" # 2 marks myfile="$2" # Verify that the input is a writable, searchable directory. # # 5 marks if [ ! -d "$mydir" ]; then echo 1>&2 "$0: '$mydir' is not a directory" exit 1 fi # 5 marks if [ ! -w "$mydir" ]; then echo 1>&2 "$0: '$mydir' is not a writable directory" exit 1 fi # 5 marks if [ ! -x "$mydir" ]; then echo 1>&2 "$0: '$mydir' 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. # newfile="$mydir/$myfile" # 5 marks if [ -s "$newfile" ]; then echo 1>&2 "$0: '$newfile' is not an empty file" exit 1 fi # Initialize the $newfile output file with the date. # Construct a temp file name using the process ID, under the tmp # directory. Put the sorted "who" output into the temp file. # Append the temp output file to the $newfile output file. # Count the number of users in the temp file and also append that. # Remove the temp file. # # 3 marks date >"$newfile" || exit $? # 3 marks tmp=/tmp/mytmp$$ # 4 marks who | sort >"$tmp" || exit $? # 3 marks cat "$tmp" >>"$newfile" || exit $? # 3 marks echo "The number of users is $(wc -l <$tmp)." >>"$newfile" # 2 marks rm "$tmp"