#!/bin/sh -u # Sample solution to Unix Final Script (neat 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 #----------------------------------------- # Input Section #----------------------------------------- # 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 #----------------------------------------- # Input Validation Section #----------------------------------------- # Step 4 - 6 marks # Step 5 - 6 marks # Step 6 - 6 marks # Make sure the pathname is not null and is a non-empty file. # if [ -z "$prog" ]; then echo 1>&2 "$0: Empty C++ source program name - nothing done. Bye!" exit 3 fi 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 if [ ! -s "$prog" ]; then echo 1>&2 "$0: C++ source file '$prog' is empty (zero size)" exit 5 fi # Step 7 - 6 marks # Add read permissions, only if necessary. # if [ ! -r "$prog" ]; then chmod ug+r "$prog" || exit 6 fi #----------------------------------------- # Processing Section #----------------------------------------- # Step 8 - 3 marks # Step 9 - 3 marks # Count the lines in the two files. # other="foo.cpp" plines=$( wc -l <"$prog" ) flines=$( wc -l <"$other" ) # Step 10 - 4 marks # Rename an existing output pathname. # outfile="happy" if [ -e "$outfile" ]; then mv "$outfile" "$outfile.bak" fi # Step 11 - 6 marks # Compile whichever file is bigger. # if [ "$plines" -gt "$flines" ] ; then infile="$prog" else infile="$other" fi g++ -o "$outfile" "$infile" || exit 8