【问题标题】:invalid arithmetic operator in if condition in bash script [duplicate]bash脚本中if条件中的算术运算符无效[重复]
【发布时间】:2020-02-27 21:50:30
【问题描述】:

我试过了:

if [[ ${numbers[4]} -ge $best_res ]]; then
                             ...
fi 

if 条件行出现以下错误:

7 ./q1.sh: line 58: [[: 0.346: syntax error: invalid arithmetic operator (error token is ".346")

有类似的问题,但我的错误是“如果”条件。如何用“bc”或“dc”修复它?

【问题讨论】:

  • Bash 不支持浮点运算。

标签: linux bash


【解决方案1】:

问题在于bash 不支持浮点运算,而这正是您的一个变量所具有的。

这是一个简单的例子:

$ [[ 0.346 -ge 0 ]] && echo "true"
bash: [[: 0.346: syntax error: invalid arithmetic operator (error token is ".346")

您必须使用bcdc 等外部工具。

【讨论】:

  • 好的,但是如果条件我怎么能有 bc
  • 使用bc,您可以这样做:$ a=0.346; b=0; echo "$a > $b" | bc 打印 0 或 1(在本例中为 1)。
猜你喜欢
  • 1970-01-01
  • 2019-04-16
  • 2017-03-21
  • 2018-08-17
  • 2013-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多