判断参数是否为数字类型

  判断参数是否为数字类型

#!/bin/bash
a=$1
b=$2

#no1 judge arg nums.
if [ $# -ne 2 ]; then
    echo "Usage: $0 int1 int2"
    exit 2
fi

#no2 judge if int
expr $a + 1 &>/dev/null 
RETVAL1=$?
expr $b + 1 &>/dev/null
RETVAL2=$?
if [ $RETVAL1 -ne 0 ] ; then
    echo "Usage: $0 int1 int2"
    exit 3
else
    if [[ $RETVAL2 -ne 0 ]]; then
        echo "Usage: $0 int1 int2"
        exit 4
    fi
fi

#no3 compart two num.
if [ $a -lt $b ]; then
    echo "$a < $b"
elif [ $a -eq $b ]; then
    echo "$a = $b"
else
    echo "$a > $b"
fi

 

上一篇:shell编程题(二十三)


下一篇:jmu-python-输入输出-格式化输出字符串