2015-09-30 57 views
2

我想在bash中使用實數來做一個while循環,但它總是給我錯誤。無論我嘗試在互聯網上找到它,它都無法解決問題。bash中的while循環中的實數

這裏是我的代碼有問題的部分:(能量是找到這樣的能源= $的命令(回聲 「$ real1 - $ REAL2」 | BC-1)

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

while (($deltaE > 0.0001)); 
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 

是否有人知道這是怎麼回事,如何解決這一問題?

+0

猛砸沒有做浮點數。使用bc或awk。 – 123

+0

但我用bc來計算$ energy和$ deltaE,所以它不應該工作? –

+1

不,bash不接受漂浮物,不管你如何製作它們。 – 123

回答

2

用BC評估比較:

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