方法一
A=123
echo "${A}" |[ -n "sed -n '/^[0-9][0-9]*$/p'" ] && echo ${A} is a numbers
方法二
A=123
echo "${A}" |grep '^[[:digit:]]*$' >/dev/null
if [ $? -eq 0 ];then
echo "${A} is a number."
else
echo '${A} Not a number.'
fi
方法三
A=123
if [ "${A}" -gt 0 ] 2>/dev/null ;then
echo "$1 is a number."
else
echo '$1 Not a number.'
fi
方法四
case "$1" in
[1-9][0-9]*)
echo "$1 is number."
;;
*)
;;
esac
方法五
A=123
echo ${A}| awk '{print($0~/^[-]?([0-9])+[.]?([0-9])+$/)?"number":"string"}'
方法六
A=123
if [ -n "$(echo ${A}| sed -n "/^[0-9]+$/p")" ];then
echo "${A} is a number."
else
echo "${A} not a number."
fi
方法七
A=123
expr ${A} "+" 1 &> /dev/null
if [ $? -eq 0 ];then
echo "${A} is a number"
else
echo "${A} not a number"
fi
若文章图片、下载链接等信息出错,请在评论区留言反馈,博主将第一时间更新!如本文“对您有用”,欢迎随意打赏,谢谢!
评论