#!/bin/sh -u # Test #3 answer - Script Section - 68 marks - 10 of 20% # One line description: # compare number of lines in file to given number and output message # Syntax: $0 number [ message_string [ file_name ] ] # Step 1 - 9-part DAT2330 format from script_style.txt - 5 marks PATH=/bin:/usr/bin ; export PATH umask 022 # Step 2 - 9 marks if [ $# -lt 1 -o $# -gt 3 ]; then echo 1>&2 "$0: Expecting number, message, and file name;" echo 1>&2 " found $# args ($*)" exit 2 fi # Step 3 - 2 marks basenum="$1" # Step 4 - 7 marks if [ "$basenum" -le 0 ] ; then echo 1>&2 "$0: basenum must be positive, found: $basenum" exit 3 fi # Step 5 - 5 marks if [ $# -ge 2 ]; then message="$2" else message='Test File' fi # Step 6 - 6 marks if [ -z "$message" ]; then echo 1>&2 "$0: 2nd argument (message) must not be null (empty string)" exit 4 fi # Step 7 - 8 marks if [ $# -ge 3 ]; then myfile="$3" else echo 1>&2 "Enter file name:" read myfile || exit 5 fi # Step 8 - 4 marks myfile=$( echo "$myfile" | tr 'A-Z' 'a-z' ) || exit 6 # Step 9 - 7 marks if [ ! -f "$myfile" ]; then echo 1>&2 "$0: '$myfile' is not an accessible, existing, plain file" exit 7 fi # Step 10 - 3 marks count=$( wc -l <"$myfile" ) # Step 11 - 2 marks echo "File '$myfile' contains $count lines" # Step 12 - 10 marks if [ $count -gt $basenum ] ; then what='larger than' elif [ $count -eq $basenum ] ; then what='the same size as' else what='smaller than' fi echo "$message $myfile is $what base number $basenum" # script exits with the exits status of the last command executed