[oracle@oracle scripts]$ cat arg_num.sh
#!/bin/sh
a=$1
b=$2
[ $# -ne 2 ]&&{
echo "USAGE:$0 NUM1 NUM2"
exit 1
}
# check if arg is number type
expr $a + 0 &>/dev/null
RETVAL1=$?
expr $b + 0 &>/dev/null
RETVAL2=$?
test $RETVAL1 -eq 0 -a $RETVAL2 -eq 0 || {
echo "please input tow "num" again."
exit 2
}
# determine which number is biger
if [ $a -lt $b ]
then
echo "$a < $b"
elif [ $a -eq $b ]
then
echo "$a = $b"
else [ $a -gt $b ]
echo "$a > $b"
fi
[oracle@oracle scripts]$ sh arg_num.sh 1 a
please input tow num again.
[oracle@oracle scripts]$ sh arg_num.sh 1 2
1 < 2
[oracle@oracle scripts]$ sh arg_num.sh 2 2
2 = 2
[oracle@oracle scripts]$ sh arg_num.sh 4 3
4 > 3
shell脚本判断传参的数字大小
最新推荐文章于 2023-11-23 17:13:59 发布