#!/bin/bash # validint--Validates integer input, allowing negative ints too. proc validint { # Validate first field and test that value against min value $2 and/or # max value $3 if they are supplied: If the value isn't within range or # it's not composed of just digits, fail. setglobal number = $1; setglobal min = $2; setglobal max = $3 if test -z $number { echo "You didn't enter anything. Please enter a number." > !2 ; return 1 } # Is the first character a '-' sign? if test $(number%${number#?}) = "-" { setglobal testvalue = $(number#?) # Grab all but the first character to test. } else { setglobal testvalue = $number } # Create a version of the number that has no digits, for testing. setglobal nodigits = $[echo $testvalue | sed 's/[[:digit:]]//g] # Check for non-digit characters. if test ! -z $nodigits { echo "Invalid number format! Only digits, no commas, spaces, etc" > !2 return 1 } if test ! -z $min { # Is the input less than the minimum value? if test $number -lt $min { echo "$number is too small: smallest acceptable value is $min" > !2 return 1 } } if test ! -z $max { # Is the input greater than the maximum value? if test $number -gt $max { echo "Your value is too big: largest acceptable value is $max" > !2 return 1 } } return 0 } # Input validation #if validint "$1" "$2" "$3" ; then # echo "That input is a valid integer value within your constraints" #fi