方法一
A=123
echo "${A}" |[ -n "sed -n '/^[0-9][0-9]*$/p'" ] && echo ${A} is a numbers
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
方法二
A=123
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
echo "${A}" |grep '^[[:digit:]]*$' >/dev/null
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
if [ $? -eq 0 ];then
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
echo "${A} is a number."
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
else
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
echo '${A} Not a number.'
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
fi
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
方法三
A=123
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
if [ "${A}" -gt 0 ] 2>/dev/null ;then
文章源自小柒网-https://www.yangxingzhen.cn/8651.html
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
Shell