============================================ Two sample solutions to script_practice2.txt ============================================ The first sample solution in this file is the "sloppy" version that 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" included later in this file. #!/bin/sh -u #------------------------------------------------------------ # 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 -------------------------------------------------------------------------- END OF FIRST (SLOPPY) SAMPLE SOLUTION -------------------------------------------------------------------------- Below is a second, improved, sample solution to script_practice2.txt. This "neat" version is a cleaned-up version of the "sloppy" version. Duplicate code has been removed. This script is easier to maintain than the "sloppy" version. #!/bin/sh -u #------------------------------------------------------------ # 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 #----------------------------------------- # Input and Validation Sections # Normally we like to keep the Input section ahead of the Validation # Section; but, it seems wise to exit the script right away if the # first argument is bad, rather than prompting for a second argument # (doing all the Input) and then exiting because the first one is bad. #----------------------------------------- # 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 #----------------------------------------- # Processing Section #----------------------------------------- # 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 # Step 12 - 4 marks # Step 13 - 7 marks # Count the characters in the two files and compare the sizes. # Tell how the size of the source file compares with the output file. # schars=$( wc -c <"$src" ) ochars=$( wc -c <"$out" ) if [ "$schars" -gt "$ochars" ] ; then diff='larger than' elif [ "$schars" -eq "$ochars" ] ; then diff='the same size as' else diff='smaller than' fi #----------------------------------------- # Output Section #----------------------------------------- echo "source $src is $diff output $out"