代码如下
#!/bin/sh
[ $# -ne 2 ] && {
echo "USAGE: $0 agr1 arg2 "
exit 1
}
expr $1 + 0 &>/dev/null
if [ $? -ne 0 ] ;then
echo "$1 is not int"
exit 1
fi
expr $2 + 0 &>/dev/null
if [ $? -ne 0 ] ;then
echo "$2 is not int"
exit
fi
if [ $1 -gt $2 ]
then
echo "$1 > $2"
elif [ $1 -eq $2 ]
then
echo "$1 = $2"
else
echo "$1 < $2"
fi
~
访问方式:
[root1@bogon shelldir]$ sh campareInt.sh 1 9
1 < 9
[root1@bogon shelldir]$ sh campareInt.sh 4 3
4 > 3
[root1@bogon shelldir]$ sh campareInt.sh a 1
a is not int
判断一个字符串是否为数字的方法
expr $1 "+" 10 &> /dev/null
if [ $? -eq 0 ];then
echo "$1 is number"
else
echo "$1 not number"
fi