#!/bin/sh -u # Print a series of numbers in a range, automatically ascending or descending. #------------------------------------------------------------ # Syntax: # $0 [ rangelow [ rangehigh [ increment ] ] ] #------------------------------------------------------------ # Purpose: # Get three inputs from the command line or by prompting and reading. # Validate all inputs as integers using a function given in class. # The first two numbers are "range" numbers; they can be anything. # Make sure the increment integer is not zero. # Output all numbers between the two range numbers, using the third # number as the increment/decrement (ascending or descending). # Exit 0 if more than one number is output. # Exit 1 if only one number was printed. # Print an error message and exit 2 on any errors. #------------------------------------------------------------- # Student Name: Ian! D. Allen # Algonquin EMail Address: alleni # Student Number: 000-000-000 # Course Number: CST8129 # Lab Section Number: 010 # Professor Name: Ian! D. Allen # Assignment Name/Number/Date: Assignment #1, script 2, due November 7 # 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 # Function definitions go here ------------------------------------ # The IsNumeric function tests its argument to see if it is a valid number. # It sets the return/exit status to zero if it is a number, to 1 if not. # IsNumeric () { # Make sure this function is called with exactly one argument. # if [ $# -ne 1 ] ; then echo 1>&2 "$0: Function IsNumeric expects one argument; found $# ($*)" exit 2 fi # The expr command sets a return code of 2 on a syntax error or # bad (non-numeric) input. Throw away all the output and errors; # look at the return status only. # [ "$1" = '-' ] && return 1 # compensate for non-number bug in expr expr 0 + "$1" >/dev/null 2>&1 # expr sets return code test "$?" -ne 2 # set return code to 0 if expr return is not 2 } # End of function definitions --------------------------------------- # If more than three arguments were given on the command line, # echo the count and incorrect input on stderr and exit. # if [ $# -gt 3 ]; then echo 1>&2 "$0: Expecting three integers; found $# args ($*)" exit 2 fi # Pick up any missing arguments by prompting on stderr and reading # them from standard input. Accept only zero, one, two, or three arguments. # The integers won't necessarily be in sorted order when entered. # if [ $# -lt 1 ]; then echo 1>&2 "Enter the first range integer:" read rangelow || exit $? else rangelow="$1" # first arg on command line becomes rangelow fi if [ $# -lt 2 ]; then echo 1>&2 "Enter the second range integer:" read rangehigh || exit $? else rangehigh="$2" # second arg on command line becomes rangehigh fi if [ $# -lt 3 ]; then echo 1>&2 "Enter the increment integer:" read increment || exit $? else increment="$3" # third arg on command line becomes increment fi # We have collected the arguments into $rangelow $rangehigh and $increment. # Optional: Echo the user's input back to the user. # echo "You entered '$rangelow' '$rangehigh' '$increment'" # Optional: Print a nice message if any input number was empty. # if [ -z "$rangelow" -o -z "$rangehigh" -o -z "$increment" ] ; then echo 1>&2 "$0: Empty input number(s); nothing done. Bye." exit 2 fi # Validate each input integer to make sure it is numeric. # if ! IsNumeric "$rangelow" ; then echo 1>&2 "$0: First range integer '$rangelow' is not numeric; exiting" exit 2 fi if ! IsNumeric "$rangehigh" ; then echo 1>&2 "$0: Second range integer '$rangehigh' is not numeric; exiting" exit 2 fi if ! IsNumeric "$increment" ; then echo 1>&2 "$0: Increment integer '$increment' is not numeric; exiting" exit 2 fi # Having validated all three variables as integers, we no longer need # to quote these variables in the rest of the script. # Validate the increment, as required. # if [ $increment -eq 0 ] ; then echo 1>&2 "$0: Increment integer '$increment' is zero; nothing done" exit 2 fi # Sort the two range integers for later use in the loop. # if [ $rangelow -gt $rangehigh ] ; then tmp=$rangelow rangelow=$rangehigh rangehigh=$tmp fi # Set up the start integer, end integer, and loop test condition for # the loop, depending on whether we are ascending or descending. # if [ $increment -gt 0 ] ; then # Ascending: start low and finish high, test for -le in loop start=$rangelow end=$rangehigh looptest="-le" else # Descending: start high and finish low, test for -ge in loop start=$rangehigh end=$rangelow looptest="-ge" fi # Loop from $start to $end, adding $increment each time. # Count how many lines of output we produce. # Since we use -le and -ge, the loop will always produce at least one output. # count=0 while [ $start $looptest $end ] ; do echo "$start" let start=start+$increment let ++count done # Exit 0 if more than one number was printed. # Exit 1 if only one number was printed. # if [ $count -gt 1 ] ; then return=0 else return=1 fi exit $return