如何在Bash中搜索小数点后的浮点数?



我在bash上搞砸了一些东西(我是新手)。

我有一个浮点数

我希望能够检查1-9的小数点后是否有数字,以确定它是否是整数-并在if-else语句中完成。

例如

if(*number does have a 1-9 digit after decimal*)
then
echo 'Number is not a whole number'
else
echo 'Number is a whole number'
fi

尝试过grep和REGEX,但还没有很好的掌握

与一个正则表达式:

x=1.123456789
if [[ "$x" =~ .[0-9]{1,9}$ ]]; then
echo 'Number is not a whole number'
else
echo 'Number is a whole number'
fi

输出:

Number不是整数
Mac_3.2.57$cat findWhole.bash
#!/bin/bash
for x in 1.123456789 0001230045600.00 1.000000 1 1. 1.. 0002 .00 22.00023400056712300 1.00 .
do
if [[ "$x" =~ ^[0-9]*.[0-9]*[1-9][1-9]*[0-9]*$ ]]; then
echo "$x is not a whole number"
elif [[ "$x" =~ ^[0-9][0-9]*.?0*$|^.00*$ ]]; then
echo "$x is a whole number"
else
echo "$x is not a number"
fi
done
Mac_3.2.57$./findWhole.bash
1.123456789 is not a whole number
0001230045600.00 is a whole number
1.000000 is a whole number
1 is a whole number
1. is a whole number
1.. is not a number
0002 is a whole number
.00 is a whole number
22.00023400056712300 is not a whole number
1.00 is a whole number
. is not a number
Mac_3.2.57$

最新更新