#!/bin/sh -u # CST8129 Final Exam Script (answer) - Fall 2002 # # Syntax: $0 fullname [ pathname ] # # Look up a full name and extract a userid from a Unix-style password file. # Look up two files in the home directory of that user and compare them. # # -IAN! idallen@ncf.ca # 1. set PATH and umask to standard values for this script # PATH=/bin:/usr/bin ; export PATH LC_COLLATE=C ; export LC_COLLATE LANG=C ; export LANG umask 022 # Collect input: # 2. take one or two command line arguments; prompt if second is missing; # issue error message for incorrect number of arguments # if [ $# -eq 2 ]; then fullname=$1 pathname=$2 elif [ $# -eq 1 ] ; then fullname=$1 echo 1>&2 "Enter the passwd file pathname" read pathname else echo 1>&2 "$0: Expecting fullname and pathname, found $# args ($*)" exit 1 fi # 3. use default Unix passwd file if user enters blank passwd file # if [ -z "$pathname" ]; then pathname='/etc/passwd' fi # Validate the input pathname: # 4. exit 2 if the pathname file is not readable # 5. exit 3 if the pathname file is not a plain file # 6. exit 4 if the pathname file is empty # if [ ! -r "$pathname" ]; then echo 1>&2 "$0: Argument '$pathname' is not a readable pathname" exit 2 fi if [ ! -f "$pathname" ]; then echo 1>&2 "$0: Argument '$pathname' is not a file" exit 3 fi if [ ! -s "$pathname" ]; then echo 1>&2 "$0: File '$pathname' is empty (zero size)" exit 4 fi # Process the name input by the user to make searching work: # 7. make the text in fullname lower-case # 8. split off the first field of fullname # 9. split off the last field (not the second field) of fullname # 10. remove all single quotes (apostrophes) from the last name # fullname=$( echo "$fullname" | tr 'A-Z' 'a-z' ) # firstname=$( echo "$fullname" \ # | sed -e 's/^[[:space:]]*\([^[:space:]]\+\).*$/\1/' ) firstname=$( echo "$fullname" | awk '{print $1}' ) # lastname=$( echo "$fullname" \ # | sed -e 's/^.*[[:space:]]\([^[:space:]]\+\)[[:space:]]*$/\1/' ) lastname=$( echo "$fullname" | awk '{print $NF}' ) # lastname=$( echo "$lastname" | sed -e "s/'//g" ) lastname=$( echo "$lastname" | tr -d "'" ) # 11. search the passwd file name field ($5) for an exact match, # last name first, one space between, and print the userid found # # userid=$( awk -F: \ # '$5 ~ /^'"$lastname $firstname"'$/ { print $1 }' "$pathname" ) userid=$( awk -F: '$5 == "'"$lastname $firstname"'" { print $1 }' "$pathname" ) # 12. exit 5 if we couldn't find the name in the passwd file # if [ -z "$userid" ] ; then echo 1>&2 "$0: Cannot find name '$lastname $firstname' in '$pathname'." exit 5 fi # 13. put a foo file name into variable foo # 14. put a bar file name into variable bar # foo="/home/$userid/*foo*" bar="/home/$userid/*bar*" # 15. exit 6 if the two names are not readable, plain files # if [ ! -f "$foo" -o ! -f "$bar" ]; then echo 1>&2 "$0: Either '$foo' or '$bar' is not a file." exit 6 fi if [ ! -r "$foo" -o ! -r "$bar" ]; then echo 1>&2 "$0: Either '$foo' or '$bar' is not readable." exit 6 fi # 16. diff the two files and show the line count of differences # 17. if the files have few differences, say so # diffcount=$( diff "$foo" "$bar" | wc -l ) echo "File \"$foo\" and \"$bar\" difference = '$diffcount' lines." if [ "$diffcount" -lt 10 ] ; then echo "Almost same: < 10 lines of difference output." fi # 18. display the first 15 lines of file $foo preceded by line numbers # # head -15 "$foo" | cat -n # cat -n "$foo" | head -15 # awk '{print NR, $0}' "$foo" | head -15 # head -15 "$foo" | awk '{print NR, $0}' awk 'NR <= 15 {print NR, $0}' "$foo"