bash - bash 中 while 循环中的实数

标签 bash

我想在 bash 中做一个 while 循环,使用实数,但它总是给我错误。无论我尝试在 Internet 上找到什么,都无法解决问题。

这是我的代码中有问题的部分:(能量是用像 energy=$(echo "$real1 - $real2"| bc -l) 这样的命令找到的

energy=${energy#-}
deltaE=$(echo "$energy"  | bc -l)
echo $energy
echo $deltaE

while (( $deltaE > 0.0001 ));
do

当然在“do”之后还有一些东西,但是执行到这里就停止了。这给了我以下错误:

1.999655175151897025
1.999655175151897025
./run_ILDA.sh: line 99: ((: 1.999655175151897025 > 0.0001 : syntax error: invalid arithmetic operator (error token is ".999655175151897025 > 0.0001 ")

没有 $ :

while (( deltaE > 0.0001 ));
do

它给了

1.999655175151897025
1.999655175151897025
./run_ILDA.sh: line 99: ((: 1.999655175151897025: syntax error: invalid arithmetic operator (error token is ".999655175151897025")

我也尝试过:

while [  $deltaE -gt 0.0001 ]
do

这给了我:

1.999655175151897025
1.999655175151897025
./run_ILDA.sh: line 99: [: 1.999655175151897025: integer expression expected

有人知道发生了什么以及如何解决这个问题吗?

最佳答案

使用bc来评估比较:

if (( $(echo "$deltaE > 0.0001" | bc -l) )); then
    ...
fi

关于bash - bash 中 while 循环中的实数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32867753/

相关文章:

mysql - 使用 bash 选择 mysql 查询

bash - 在波浪号之前扩展变量

linux - 为什么不能使用 cat 逐行读取文件,其中每行都有分隔符

linux - 无法在 Debian Squeeze 中检查 cups 打印机状态

bash - 如何在 Perl CGI 脚本中从用户的 .bashrc 获取和使用用户的 TZ 设置?

bash - ${VAR :=value} in a bash script 的结果是什么

linux - 设备如何通过数据链路层 2 交换数据?

linux - 如何从 bash 中的 df 中提取特定数字?

bash - 在 Bash shell 脚本中传播所有参数

bash - 在 Bash 中,grep 查找一行并将该行打印到文件中,但包含更多信息