入力が整数値として有効かどうか確認する

#!/bin/sh
#validint.sh

validint(){
    #2番目3番目の引数が指定されていればそれぞれ値の範囲の上限と下限として
    #扱いその範囲に収まっているかもチェック。2番目以降の引数が指定されて
    #いなければ値の範囲チェックは行わない

    number="$1"
    min="$2"
    max="$3"

    if [ -z $number ]; then
        echo "You didn't enter anything. Unacceptable." >&2
        return 1
    fi
    
    if [ "${number%${number#?}}" = "-" ]; then #最初の文字が-か
        testvalue="${number#?}"                #最初の文字を除く
    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
}

if validint "$1" "$2" "$3" ; then
    echo "That input is a valid integer value within your constraints"
fi
$ ./validint.sh 1234.3
Invalid number format! Only digits, no commas, spaces, etc.

$ ./validint.sh 103 1 100
Your value is too big: largest acceptable value is 100

$ ./validint.sh -17  0 25
Your value is too small: smallest acceptable value is 0

$ ./validint.sh -17  -20 25
That input is a valid integer value within your constraints