#!/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

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. nothing done" exit 2 fi if ! IsNumeric "$int2" . # echo "You entered '$int1' '$int2' '$int3'" # Optional: Print a nice message if any input number was empty. Bye. then echo 1>&2 "$0: Third input number '$int3' is not numeric. # if [ -z "$int1" -o -z "$int2" -o -z "$int3" ] . then # first arg on command line becomes int1 # second arg on command line becomes int2 # third arg on command line becomes int3 "$0: Expecting three integers.int1="$1" echo 1>&2 read int2 elif [ $# -eq int1="$1" int2="$2" echo 1>&2 read int3 elif [ $# -eq int1="$1" int2="$2" int3="$3" else echo 1>&2 exit 2 fi # first arg on command line becomes int1 "Enter two integers:" int3 || exit $? 2 ]. found $# args ($*)" # We have collected the three arguments into $int1 $int2 and $int3. then echo 1>&2 "$0: First input number '$int1' is not numeric. nothing done" exit 2 fi if ! IsNumeric "$int3" . # sorted1=$int1 sorted2=$int2 sorted3=$int3 # Two compares and swaps make sorted3 the largest integer. # Echo the user's input back to the user. nothing done. then echo 1>&2 "$0: Empty integer input. then echo 1>&2 "$0: Second input number '$int2' is not numeric." exit 2 fi # Validate each input integer to make sure it is numeric. then # first arg on command line becomes int1 # second arg on command line becomes int2 "Enter third integer:" || exit $? 3 ]. then tmp=$sorted1 sorted1=$sorted2 sorted2=$tmp fi . # Transfer the inputs to other variables and then bubble sort them. # if [ $sorted1 -gt $sorted2 ] . # if ! IsNumeric "$int1" . as specified.

then return=0 else return=1 fi exit $return .if [ $sorted2 -gt $sorted3 ] . sort the remaining two. 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 ] . # if [ $sorted1 -gt $sorted2 ] . then tmp=$sorted2 sorted2=$sorted3 sorted3=$tmp fi # sorted3 now has the biggest integer.

Sign up to vote on this title
UsefulNot useful