#!/bin/sh -u # Sample solution to Unix Final Script (sloppy version) #------------------------------------------------------------ # One line summary: Compile the larger of two C++ programs #------------------------------------------------------------ # Syntax: # $0 [ sourcefilename ] #------------------------------------------------------------ # Purpose: not required #------------------------------------------------------------- # Assignment Label: not required #------------------------------------------------------------- # Step 1 - 4 marks - correct header (syntax, interpreter, 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 - 5 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 - 6 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 - 6 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 - 3 marks # Count the lines in the user-supplied file. # plines=$( wc -l <"$prog" ) # Step 9 - 3 marks # Count the lines in the existing file. # flines=$( wc -l <"foo.cpp" ) # Step 10 - 4 marks # Rename an existing output pathname. # if [ -e "happy" ]; then mv "happy" "happy.bak" fi # Step 11 - 6 marks # Compile whichever file is bigger. # if [ "$plines" -gt "$flines" ] ; then g++ -o "happy" "$prog" || exit 8 else g++ -o "happy" "foo.cpp" || exit 8 fi