shell⽐较两个整数⼤⼩并严格判断参数输⼊脚本代码如下
#!/bin/sh
[ $#-ne2 ] && {
echo "USAGE: $0 agr1 arg2 "
exit1
shell代码}
expr $1 + 0 &>/dev/null
if [ $? -ne0 ] ;then
echo "$1 is not int"
exit1
fi
expr $2 + 0 &>/dev/null
if [ $? -ne0 ] ;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 19
1 < 9
[root1@bogon shelldir]$ sh campareInt.sh 43
4 > 3
[root1@bogon shelldir]$ sh campareInt.sh a 1
a is not int
判断⼀个字符串是否为数字的⽅法
expr $1"+"10 &> /dev/null
if [ $? -eq0 ];then
echo "$1 is number"
else
echo "$1 not number"
fi
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论