我想在 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/