#!/bin/sh -u # Classify a file as small, medium, large. #------------------------------------------------------------ # Syntax: # $0 filename #------------------------------------------------------------ # Purpose: # Classify the filename argument based on the number of lines: # 0-99 = small 100-499 = medium over 499 = large # Print an error message and exit non-zero on any errors. #------------------------------------------------------------- # Student Name: Ian! D. Allen # Algonquin EMail Address: alleni # Student Number: 000-000-000 # Course Number: DAT2330 # Lab Section Number: 010 # Professor Name: Ian! D. Allen # Assignment Name/Number/Date: Sample Assignment Label # Comment: This is a sample assignment label. # see http://idallen.com/teaching/assignment_standards.html #------------------------------------------------------------- # Set standard search path and umask. # PATH=/bin:/usr/bin ; export PATH umask 022 # Define the two line count points that distinguish small/medium/large. # small=100 smallclass='small' medium=1000 mediumclass='medium' largeclass='large' # Make sure we have exactly one argument. # If not, echo the count and incorrect input back to the user on stderr. # if [ $# -ne 1 ] ; then echo 1>&2 "$0: Expecting 1 filename argument, found $# ($*)" exit 1 fi # Optional: Echo the user's input argument back to the user before using it. # echo "Your filename is: $1" # Don't allow an empty argument. # if [ -z "$1" ] ; then echo 1>&2 "$0: Expecting 1 filename argument; but, your argument is empty." exit 1 fi # Validate the argument as being a readable file. # Optional: Detect directories and issue a special message for them. # if [ -d "$1" ] ; then echo 1>&2 "$0: argument '$1' is a directory, not a file." exit 1 fi if [ ! -f "$1" ] ; then echo 1>&2 "$0: argument '$1' is not an accessible file." exit 1 fi if [ ! -r "$1" ] ; then echo 1>&2 "$0: file '$1' is not readable." exit 1 fi # Count the lines in the file. Redirect stdin so that wc doesn't # print a file name as well as the number of lines. Conditionally # exit the script if the wc fails for any reason. # lines=$( wc -l <"$1" ) || exit $? # You could use an "elif" in this block of code. # if [ $lines -lt $small ] ; then class=$smallclass else if [ $lines -lt $medium ] ; then class=$mediumclass else class=$largeclass fi fi echo "File '$1' contains $lines lines and is: $class" exit 0