#!/bin/sh -u # Script Practice #4 answer - 69 marks #------------------------------------------------------------ # One line summary: Compile a program as C or C++ based on its contents #------------------------------------------------------------ # Syntax: $0 [ source_file ] #------------------------------------------------------------ # Purpose: not required #------------------------------------------------------------ # Assignment Label: not required #------------------------------------------------------------ # -IAN! idallen@idallen.ca # Step 1 - 6 marks - script header # (interpreter, one-line summary, syntax, PATH, umask, collate) # Use a standard shell search path and friendly umask. # Use standard ASCII sort ordering. # PATH=/bin:/usr/bin ; export PATH umask 022 LC_COLLATE=C ; export LC_COLLATE # Step 2 - 7 marks # Validate the number of arguments; issue a good error message. # if [ $# -gt 1 ]; then echo 1>&2 "$0: Expecting one optional source file; found $# ($*)" exit 1 fi # Step 3 - 7 marks # Prompt (always on stderr) for a missing program source file. # Put the file name into a named variable. # if [ $# -eq 0 ]; then echo 1>&2 "Enter the program source file name:" read myprog || exit 1 else myprog="$1" fi # Step 4 - 2 marks echo "Processing file '$myprog'." # Step 5 - 14 marks # Verify that the input is a file and is readable. # if [ ! -f "$myprog" ]; then echo 1>&2 "$0: '$myprog' is not a file" exit 1 fi if [ ! -r "$myprog" ]; then echo 1>&2 "$0: '$myprog' is not readable" exit 1 fi # Step 6 - 10 marks # Select the C++ compiler if a string is found indicating a C++ program. # if grep '/dev/null ; then # g++ -Wall -o newcpp "$myprog" 2>warnings.out # cstatus=$? # fi # Step 7 - 2 marks # Select the C compiler if a string is found indicating a C program. # if grep '/dev/null ; then # gcc -Wall -o newccc "$myprog" 2>warnings.out # cstatus=$? # fi # Step 8 - 7 marks # combine the above into one step: # if grep '/dev/null ; then compiler=g++ outfile=newcpp elif grep '/dev/null ; then compiler=gcc outfile=newccc else echo 1>&2 "$0: your source file '$myprog' cannot be compiled" exit 1 fi "$compiler" -Wall -o "$outfile" "$myprog" 2>warnings.out cstatus=$? # Step 9 - 3 marks if [ $cstatus -ne 0 ]; then echo "Compiler $compiler exit status was $cstatus." fi # Step 10 - 4 marks # Remove an empty error file and exit zero. # if [ ! -s warnings.out ]; then rm warnings.out echo "Output is in $outfile" exit 0 fi # Step 11 - 7 marks # Rename file and display count of errors on stdout. # mv warnings.out warnings.txt chmod 400 warnings.txt lines=$( wc -l