bash - 如何在bash中对浮点变量求和?

标签 bash shell while-loop addition

我正在尝试运行一个脚本,其中每次在 while 循环中通过“作为用户输入给出”的 float 来更新变量。我知道 bash 不支持 float ,我尝试使用 | bc 但它似乎对我不起作用......

step_l0=0.25
step_l=0
while [ $step_l -le 2 ] do
  step_r=$radius_input
  while [ $step_r -le $radius_define ] do

    stuff done in the while loops

    step_r=$(( $step_r + $step_r0 ))
  done
  step_l=$(( $step_l + $step_l0 ))
done

最佳答案

chepner的建议-

$: a=0.25
$: b=1.753
$: awk "BEGIN{ print $a + $b }"
2.003
$: python -c "print $a + $b"
2.003
$: perl -e "print $a + $b"$'\n'
2.003

在可能的情况下(实际上总是可能的),如果您需要调用更精确或更强大的语言来完成脚本中的任务,请考虑转换整个脚本。你会很高兴你这么做了。

关于bash - 如何在bash中对浮点变量求和?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59140756/

相关文章:

windows - 如何解释批处理字符串中的变量?

linux - sed 命令仅过滤后跟逗号的一组数字

java - 循环计算字符串中给定字符数的问题

java - 没有尾部字段的 LinkedList 陷入无限循环

linux - 如何使用用户输入的绝对路径使用 tar 来备份目录?

ruby - 首次登录时在 heroku 中获取 "Authentication Failed"

bash - 如何只选择我的 AWK 脚本中的前 10 行

bash - 将定界符后的所有参数传递给另一个命令

linux - Unix,替换错误错误?

java - 要求用户使用简单的 while 循环再次运行程序