判断输入脚本的数字是不是整数expr

[root@web02-7 scripts]# cat calc.sh 
#!/bin/bash
 [ $# -ne 2 ] && {
  echo "you must input tow num:$0 arg1 arg2"
  exit 1
}
expr $1 + 0 &>/dev/null
[ $? -eq 0 ] ||{
  echo "you must input $1 rigth num"
  exit 2
}
expr $2 + 0 &>/dev/null
[ $? -eq 0 ] ||{
  echo "you must input $2 rigth num"
  exit 3
}
[root@web02-7 scripts]# sh -x calc.sh
+ '[' 0 -ne 2 ']'
+ echo 'you must input tow num:calc.sh arg1 arg2'
you must input tow num:calc.sh arg1 arg2
+ exit 1
[root@web02-7 scripts]# sh -x calc.sh a b
+ '[' 2 -ne 2 ']'
+ expr a + 0
+ '[' 2 -eq 0 ']'
+ echo 'you must input a rigth num'
you must input a rigth num
+ exit 2
[root@web02-7 scripts]# sh -x calc.sh 1 2
+ '[' 2 -ne 2 ']'
+ expr 1 + 0
+ '[' 0 -eq 0 ']'
+ expr 2 + 0
+ '[' 0 -eq 0 ']'

  

[root@web02-7 scripts]# cat calc1.sh 
#!/bin/bash
if [ $# -ne 2 ]
then
  echo "USAGE:$0 arg1 arg2"
  exit 1
fi
expr $1 + $2 + 1 &>/dev/null
if [ $? -ne 0 ]
then
  echo "USAGE:$0 int num"
  exit 2
fi
[root@web02-7 scripts]# sh -x calc1.sh 2 5
+ '[' 2 -ne 2 ']'
+ expr 2 + 5 + 1
+ '[' 0 -ne 0 ']'

  

posted @ 2017-02-27 10:53  reborn枪  阅读(267)  评论(0)    收藏  举报