#!/bin/bash -u # Put two integers in ascending order. Prompt for missing arguments. #------------------------------------------------------------ # Syntax: # $0 [ integer1 [ integer2 ] ] #------------------------------------------------------------ # Purpose: # Get two inputs from the command line or by prompting and reading. # Validate them as integers using a function given in class. # Output a line containing the smaller number followed by the larger. # 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: Exercise #6, script 6, due October 22 # 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, or two arguments. # If more than 2, echo the count and incorrect input on stderr and exit. # case "$#" in 0) echo 1>&2 "Enter two integers:" read int1 int2 ;; 1) int1="$1" echo 1>&2 "Enter one integer:" read int2 ;; 2) int1="$1" int2="$2" ;; *) echo 1>&2 "$0: Expecting 0-2 integer arguments, found $# ($*)" exit 1 ;; esac # Optional: Print a nice message if either input number was empty. # if [ -z "$int1" -o -z "$int2" ] ; then echo 1>&2 "$0: Empty integer input; nothing done. Bye." exit 1 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." exit 1 fi if ! IsNumeric "$int2" ; then echo 1>&2 "$0: Second input number '$int2' is not numeric." exit 1 fi # Copy the inputs and put the two numbers in order, so $big is biggest. # small=$int1 big=$int2 if [ $small -gt $big ] ; then tmp=$small small=$big big=$tmp fi echo "You entered: $int1 and $int2. In order, they are: $small $big" exit 0