#!/bin/sh -u # Pluralize the string if the number is not one. # Syntax: # $0 number string # Purpose: # If the number is not singular, output the string with an "s" on the end. # This knows nothing about English spelling, so it will make mistakes # on many plurals (e.g. mouse, box, concerto, etc.). # -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 if [ $# -ne 2 ] ; then echo 1>&2 "$0: Expecting number and string, found $# arguments ($*)" exit 2 fi # Transfer the two arguments to named variables to improve readability. # number="$1" string="$2" # 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 nonzero 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 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 test "$?" -ne 2 # set return code to 0 if true } # See if the number really is numeric. # if ! IsNumeric "$number" ; then echo 1>&2 "$0: Number '$number' is not an integer; nothing done" exit 2 fi # See if the string is not empty. # if [ -z "$string" ] ; then echo 1>&2 "$0: String is empty; nothing done" exit 2 fi # pseudo-English pluralization # if [ $number -eq 1 -o $number -eq -1 ] ; then plural='' else plural='s' fi echo "${string}${plural}" exit 0