#!/bin/sh -u # Sorts three integers on the command line; prompts for missing numbers. #------------------------------------------------------------ # Syntax: # $0 [ int1 [ int2 [ int3 ] ] ] #------------------------------------------------------------ # Purpose: # Get three inputs from the command line or by prompting and reading. # Validate all inputs as integers using a function given in class. # Output the integer numbers in ascending order. # Exit zero if the numbers were already sorted; exit 1 otherwise. # 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: CST8129 # Lab Section Number: 010 # Professor Name: Ian! D. Allen # Assignment Name/Number/Date: Assignment #1, script 1, 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 --------------------------------------- # Pick up any missing arguments by prompting on stderr and reading # them from standard input. Accept only zero, one, two, or three arguments. # If more than three, echo the count and incorrect input on stderr and exit. # if [ $# -eq 0 ]; then echo 1>&2 "Enter three integers:" read int1 int2 int3 || exit $? elif [ $# -eq 1 ]; then int1="$1" # first arg on command line becomes int1 echo 1>&2 "Enter two integers:" read int2 int3 || exit $? elif [ $# -eq 2 ]; then int1="$1" # first arg on command line becomes int1 int2="$2" # second arg on command line becomes int2 echo 1>&2 "Enter third integer:" read int3 || exit $? elif [ $# -eq 3 ]; then int1="$1" # first arg on command line becomes int1 int2="$2" # second arg on command line becomes int2 int3="$3" # third arg on command line becomes int3 else echo 1>&2 "$0: Expecting three integers; found $# args ($*)" exit 2 fi # We have collected the three arguments into $int1 $int2 and $int3. # Echo the user's input back to the user, as specified. # echo "You entered '$int1' '$int2' '$int3'" # Optional: Print a nice message if any input number was empty. # if [ -z "$int1" -o -z "$int2" -o -z "$int3" ] ; then echo 1>&2 "$0: Empty integer input; nothing done. Bye." exit 2 fi # Validate each input integer to make sure it is numeric. # if ! IsNumeric "$int1" ; then echo 1>&2 "$0: First input number '$int1' is not numeric; nothing done" exit 2 fi if ! IsNumeric "$int2" ; then echo 1>&2 "$0: Second input number '$int2' is not numeric; nothing done" exit 2 fi if ! IsNumeric "$int3" ; then echo 1>&2 "$0: Third input number '$int3' is not numeric; nothing done" exit 2 fi # Having validated all three variables as integers, we no longer need # to quote these variables in the rest of the script. # Transfer the inputs to other variables and then bubble sort them. # sorted1=$int1 sorted2=$int2 sorted3=$int3 # Two compares and swaps make sorted3 the largest integer. # if [ $sorted1 -gt $sorted2 ] ; then tmp=$sorted1 sorted1=$sorted2 sorted2=$tmp fi if [ $sorted2 -gt $sorted3 ] ; then tmp=$sorted2 sorted2=$sorted3 sorted3=$tmp fi # sorted3 now has the biggest integer; sort the remaining two. # if [ $sorted1 -gt $sorted2 ] ; then tmp=$sorted1 sorted1=$sorted2 sorted2=$tmp fi echo "The numbers are, from smallest to largest: $sorted1 $sorted2 $sorted3" # Return 0 if the numbers were already sorted # if [ $int1 -eq $sorted1 -a $int2 -eq $sorted2 -a $int3 -eq $sorted3 ] ; then return=0 else return=1 fi exit $return