5 Validating Integer Input


#5 Validating Integer Input

As you saw in Script #2, validating integer input seems like a breeze until you want to ensure that negative values are acceptable too. The problem is that each numeric value can have only one negative sign, which must come at the very beginning of the value. The validation routine in this script makes sure that negative numbers are correctly formatted, and, to make it more generally useful, it can also check that values are within a range specified by the user .

The Code

 #!/bin/sh # validint -- Validates integer input, allowing negative ints too. function validint {   # Validate first field. Then test against min value  and/or   # max value  if they are supplied. If they are not supplied, skip these tests.   number="";      min="";      max=""   if [ -z $number ] ; then     echo "You didn't enter anything. Unacceptable." >&2 ; return 1   fi   if [ "${number%${number#?}}" = "-" ] ; then  # is first char a '-' sign? testvalue="${number#?}"     # all but first character   else     testvalue="$number"   fi   nodigits="$(echo $testvalue  sed 's/[[:digit:]]//g')"   if [ ! -z $nodigits ] ; then     echo "Invalid number format! Only digits, no commas, spaces, etc." >&2     return 1   fi   if [ ! -z $min ] ; then     if [ "$number" -lt "$min" ] ; then        echo "Your value is too small: smallest acceptable value is $min" >&2        return 1     fi   fi   if [ ! -z $max ] ; then      if [ "$number" -gt "$max" ] ; then        echo "Your value is too big: largest acceptable value is $max" >&2        return 1      fi   fi   return 0 } 

Running the Script

This entire script is a function that can be copied into other shell scripts or included as a library file. To turn this into a command, simply append the following to the bottom of the script:

 if validint "" "" "" ; then   echo "That input is a valid integer value within your constraints" fi 

The Results

 $  validint 1234.3  Invalid number format! Only digits, no commas, spaces, etc. $  validint 103 1 100  Your value is too big: largest acceptable value is 100 $  validint -17 0 25  Your value is too small: smallest acceptable value is 0 $  validint -17 -20 25  That input is a valid integer value within your constraints 

Hacking the Script

Notice in this script the following test to see if the number's first character is a negative sign:

 if [ "${number%${number#?}}" = "-" ] ; then 

If the first character is a negative sign, testvalue is assigned the numeric portion of the integer value. This nonnegative value is then stripped of digits, and what remains is tested further.

You might be tempted to use a logical AND to connect expressions and shrink some of the nested if statements. For example, it seems as though the following should work:

 if [ ! -z $min -a "$number" -lt "$min" ] ; then      echo "Your value is too small: smallest acceptable value is $min" >&2      exit 1   fi 

However, it doesn't work because you can't guarantee in a shell script AND expression that the second condition won't be tested if the first proves false. It shouldn't be tested, but . . .




Wicked Cool Shell Scripts. 101 Scripts for Linux, Mac OS X, and Unix Systems
Wicked Cool Shell Scripts
ISBN: 1593270127
EAN: 2147483647
Year: 2004
Pages: 150
Authors: Dave Taylor

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net