运维网's Archiver
论坛
›
Shell/Powershell
› Linux Shell 条件表达式判断是否为数字
坏气十足
发表于 2018-8-25 09:29:45
Linux Shell 条件表达式判断是否为数字
#判断输入的字符串是否为数字
function testNumber() {
if [[ "$1" =~ ^(-?|\+?)+(\.?+)?$ ]]
then
echo yes
else
echo no && exit 1
fi
}
echo "输入一个非数字字符将退出"
while (true)
do
read a
testNumber $a
done
说明:
~ 表示逐位匹配,存在返回0,不存在返回非0
页:
[1]
查看完整版本:
Linux Shell 条件表达式判断是否为数字