#!/bin/sh -u # Validate a shell script professor comment line. # ------------------------------------------------------------------ # Syntax: # $0 pathname # ------------------------------------------------------------------ # Purpose: Make sure the script has a valid Professor comment line. # Print appropriate error messages. Exit 2 if not valid. # ------------------------------------------------------------------ # The following line is here so that this program can validate itself: # Professor Name: Dennis Ritchie and Brian Kernighan echo "*** MISSING CODE MUST BE ADDED HERE (omitted for brevity in this example):" # - Insert code here to set PATH and umask. # - Insert code here to prompt for and read a missing command line argument. # - Insert code here to validate the $file (is a file, readable, etc.) file="$1" # transfer first argument to a more readable variable name #------------------------------------------------------------------------ # Start of function definition section: # Function #1: DennisBrianTester # Arguments: 1 (a script file name) # # This testing function looks for the "Professor Name" comment that # should be in the top 20 lines of the file whose name is given # as the first argument to the function. If the comment line is # found, the function makes sure the correct professor name is given. # # To select the "Professor Name" line, we select the first 20 lines of # the script file (first argument) and run the lines through the following # egrep extended regexp: # # - start at the beginning of the line # - allow any number of blanks around the comment "#" # - allow one or more blanks between the two words Professor Name # - accept upper or lower case leading letters on the words # - the trailing colon after Name: is optional # # If egrep finds the Professor Name line based on the above regular expression, # the function will do the following further tests on the line found: # - make sure the professor name line contains Dennis Ritchie # - make sure the professor name line contains Brian Kernighan # Return status 0 if all the tests pass, non-zero otherwise. # DennisBrianTester () { # Select the first 60 lines of the file (function argument 1) only. # Select only the first of the lines found (egrep might find many). # Put the egrep regexp into a variable so we can use it twice # without writing it twice - does not duplicate code. # Save the output of the egrep in variable $line for later use. # regexp='^ *# *[Pp]rofessor +[Nn]ame:?' line=$( head -60 "$1" | egrep "$regexp" | head -1 ) # See if the egrep pattern found the line (test for zero size string): # if [ -z "$line" ] ; then echo 1>&2 "$0: '$1': Missing Professor Name comment" return 1 # line not found - return bad status (does not exit) fi # If we get here, the egrep pattern must have found the comment line. # The line is saved in the shell variable $line. # Test the line for the name of the professor: # See if the line we found has name Dennis Ritchie anywhere in it. # This re-uses the same regexp pattern from above and adds to it. # [[:space:]] is a POSIX character class matching white space. # Note the use of both double and single quotes in the pattern argument. # Note that we echo the $line as part of the error message. # try=$( echo "$line" | egrep \ "$regexp"'[[:space:]]+.*[Dd]ennis +[Rr]itchie *$' ) if [ -z "$try" ] ; then echo 1>&2 "$0: '$1': Missing name Dennis Ritchie: $line" return 2 # return bad status (does not exit) fi # See if the line we found has name Brian Kernighan anywhere in it. # This re-uses the same regexp pattern from above and adds to it. # [[:space:]] is a POSIX character class matching white space. # Note the use of both double and single quotes in the pattern argument. # Note that we echo the $line as part of the error message. # try=$( echo "$line" | egrep \ "$regexp"'[[:space:]]+.*[Bb]rian +[Kk]ernighan *$' ) if [ -z "$try" ] ; then echo 1>&2 "$0: '$1': Missing name Brian Kernighan: $line" return 2 # return bad status (does not exit) fi return 0 # no errors - must be a valid line - return good status } # End of function definitions. #------------------------------------------------------------------------ # Start of actual script code. # Here we call the function, passing it the script file name as its only # argument. If the function returns a bad status, we exit with return # code 2. (The function printed the error message for us.) If the # function found the line and validated it and returned a good status, # we exit with return code zero. # if ! DennisBrianTester "$file" ; then exit 2 fi echo "The file '$file' contains a correct Professor Name comment line." exit 0