#!/bin/sh -u # Sample solution to Unix Final Script (sloppy version) # This "sloppy" version is what you might write by following the # test specifications literally, without thinking about making your # code maintainable. For a better solution, see the "neat version". #------------------------------------------------------------ # One line summary: compile a file and compare the source and output sizes #------------------------------------------------------------ # Syntax: # $0 sourcefilename [ outputfilename ] #------------------------------------------------------------ # Purpose: not required for test #------------------------------------------------------------- # Assignment Label: not required for test #------------------------------------------------------------- # Step 1 - 5 marks - interpreter, one line comment, syntax, PATH, and umask # Set a standard search path and non-restrictive umask. # PATH=/bin:/usr/bin ; export PATH umask 022 # Step 2 - 8 marks # Exit the script if less than 1 or more than 2 arguments. # if [ $# -lt 1 -o $# -gt 2 ]; then echo 1>&2 "$0: expecting source file and optional output file," echo 1>&2 " found $# ($*)" exit 1 fi # Step 3 - 2 marks # Put the source file name (first argument) into a variable. # src="$1" # Step 4 - 7 marks # Exit if the pathname is not a plain file. # if [ ! -f "$src" ]; then echo 1>&2 "$0: C++ source '$src' is missing, inaccessible," echo 1>&2 " or not a plain file" exit 2 fi # Step 5 - 7 marks # Exit if the file is empty (zero size - contains no data). # if [ ! -s "$src" ]; then echo 1>&2 "$0: C++ source file '$src' is empty (zero size)" exit 3 fi # Step 6 - 8 marks # Read the output file name from the user if the argument is missing. # if [ $# -eq 2 ]; then out="$2" else echo 1>&2 "Enter an output file name:" read out || exit 4 fi # Step 7 - 6 marks # Exit if the pathname is an empty string (null string - no characters). # if [ -z "$out" ]; then echo 1>&2 "$0: Empty output file name - nothing done. Bye!" exit 5 fi # Step 8 - 4 marks # Add read permissions only for group. # chmod g+r "$src" || exit 6 # Step 9 - 4 marks # Rename an existing output pathname. # if [ -e "$out" ]; then mv "$out" "backup.bak" fi # Step 10 - 4 marks # Compile the C++ source into the output file with warnings enabled # g++ -o "$out" -Wall "$src" # Step 11 - 4 marks # Count the characters in the source file. # schars=$( wc -c <"$src" ) # Step 12 - 4 marks # Count the characters in the output file. # ochars=$( wc -c <"$out" ) # Step 13 - 7 marks # Tell how the size of the source file compares with the output file. # if [ "$schars" -gt "$ochars" ] ; then echo "source $src is larger than output $out" else if [ "$schars" -eq "$ochars" ] ; then echo "source $src is the same size as output $out" else echo "source $src is smaller than output $out" fi fi