#!/bin/sh -u # Test #4 answer - Script Part II - 70 marks - 11 of 35% #------------------------------------------------------------ # One line summary: Compile the larger of two C++ programs #------------------------------------------------------------ # Syntax: $0 [ sourcefilename ] #------------------------------------------------------------ # Purpose: not required #------------------------------------------------------------- # Assignment Label: not required #------------------------------------------------------------- # -IAN! idallen@idallen.ca # Step 1 - 5 marks - script header # (interpreter, one-line summary, syntax, PATH, and umask) # Set a standard search path and non-restrictive umask. # PATH=/bin:/usr/bin ; export PATH 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 C++ source filename argument," echo 1>&2 "found $# ($*)" exit 1 fi # Step 3 - 7 marks # Read the program name from the user if the argument is missing. # if [ $# -eq 1 ]; then prog="$1" else echo 1>&2 "Enter a C++ source file:" read prog || exit 2 fi # Step 4 - 6 marks # Exit if the pathname is an empty string (null string - no characters). # if [ -z "$prog" ]; then echo 1>&2 "$0: Empty C++ source program name - nothing done. Bye!" exit 3 fi # Step 5 - 7 marks # Exit if the pathname is not a plain file. # if [ ! -f "$prog" ]; then echo 1>&2 "$0: C++ source '$prog' is missing, inaccessible," echo 1>&2 "or not a plain file" exit 4 fi # Step 6 - 7 marks # Exit if the file is empty (zero size - contains no data). # if [ ! -s "$prog" ]; then echo 1>&2 "$0: C++ source file '$prog' is empty (zero size)" exit 5 fi # Step 7 - 6 marks # Add missing read permissions. # if [ ! -r "$prog" ]; then chmod ug+r "$prog" || exit 6 fi # Step 8 - 4 marks # Count the lines in the user-supplied file. # plines=$( wc -l <"$prog" ) || exit 7 # Step 9 - 4 marks # Count the lines in the existing file. # flines=$( wc -l <"foo.cpp" ) || exit 8 # Step 10 - 4 marks # Rename an existing output pathname. # if [ -e "happy" ]; then mv "happy" "happy.bak" fi # Step 11 - 8 marks # Compile whichever file is bigger into "happy". # if [ "$plines" -gt "$flines" ] ; then bigger="$prog" else bigger="foo.cpp" fi g++ -o "happy" "$bigger" || exit 9 # Step 12 - 5 marks # Tell the user if the Makefile contained the larger program name. # if [ -r Makefile ] && grep "$bigger" Makefile >/dev/null ; then echo "You should use 'make' next time - $bigger is in the Makefile" fi