在bash中有条件地浮动 [英] Float conditional in bash

查看:66
本文介绍了在bash中有条件地浮动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在bash中,我需要比较两个浮点数,为此,我在脚本中定义了一个浮点数,而另一个则作为参数读取:

in bash I need to compare two float numbers, one which I define in the script and the other read as paramter, for that I do:

   if [[ $aff -gt 0 ]]
    then
            a=b
            echo "xxx "$aff
            #echo $CX $CY $CZ $aff
    fi

但是我得到了错误:

[[:: -309.585300:语法错误:无效的算术运算符(错误标记为".585300")

[[: -309.585300: syntax error: invalid arithmetic operator (error token is ".585300")

怎么了?

谢谢

推荐答案

使用awk

#!/bin/bash
num1=0.3
num2=0.2
if [ -n "$num1" -a -n "$num2" ];then
  result=$(awk -vn1="$num1" -vn2="$num2" 'BEGIN{print (n1>n2)?1:0 }')
  echo $result
  if [ "$result" -eq 1 ];then
   echo "$num1 greater than $num2"
  fi
fi

这篇关于在bash中有条件地浮动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆