其他分享
首页 > 其他分享> > 判断参数是否为数字类型

判断参数是否为数字类型

作者:互联网

  判断参数是否为数字类型

#!/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

 

标签:判断,数字,ne,echo,exit,参数,Usage,fi,int1
来源: https://www.cnblogs.com/bjx2020/p/11098124.html