Linux bash shell 的浮点数比较
在bash shell脚本中不直接支持浮点数的比较,比如
a=7.2
b=8
if [ $a -lt $b ] ; then
echo "a less than b "
else
echo "b less than a "
fi
会报错 integer expression expected
有两种解决办法
bc 方法
if [ `echo "$a < $b"|bc` -eq 1 ] ; then
awk 方法
if [ `echo "$a $b" | awk '{if($1<$2) {print 0} else {print 1}}'` -eq 0 ] ; then
来个实际用的例子,我打算检查ubuntu是否安装了vim-full,版本是否超过7.0
result=`dpkg -l | grep "vim-full"`
if [ -n "$result" ]; then
ver_vim=`vim --version | grep "^VIM" | awk '{print $5}' `
echo "you have installed vim-full $ver_vim "
if [ `echo "$ver_vim > 7"|bc` -eq 1 ] ; then
echo "all is ok "
else
echo "vim's version is so low "
fi
else
echo "don't install vim-full, will auto install"
fi
顶(0)
踩(0)
上一篇:Ubuntu 命令行启动
下一篇:ubuntu 设置固定 ip
- 最新评论