#!/bin/sh -u # Demonstrate the use of a shell function to set a return code. # Syntax: # $0 string # Purpose: # Test to see if the single string argument is all-numeric. # Print a message on stdout or an error message on stderr. # -IAN! idallen@ncf.ca # Set the search path for the shell to be the standard places. # Set the umask to be non-restrictive and friendly to others. # PATH=/bin:/usr/bin ; export PATH umask 022 # 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 IsNumeric: Expecting one function argument; found $# ($*)" exit 1 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 } # Make sure this script is called with exactly one argument. # if [ $# -ne 1 ] ; then echo 1>&2 "$0: Expecting one argument; found $# ($*)" exit 1 fi # See if the command line argument is numeric. # if IsNumeric "$1" ; then echo "First argument '$1' is numeric." else echo 1>&2 "$0: First argument '$1' is not numeric." fi