2014-01-10 35 views
2

請幫我找出這個問題,我是很新的UNIX, 我的代碼是真的不能得到正確的答案...使用bash shell的浮點在UNIX

#! /usr/bin/env bash 

echo -n "How many number : "; read num 
for ((i=0; i<$num; i++)); 
do 
    echo -n "Enter your number : "; read number 
total+=$(echo "${number}" | bc) 
echo "${total}" 

done 
+0

#!/usr/bin/env bash echo -n「多少個數:」;對於((i = 0; i <$ num; i ++)),讀取編號爲 ; do \t echo -n「輸入您的號碼:」;閱讀數\t total + = $(echo「$ {number}」| bc) echo「$ {total}」 done – user3183246

+0

請修改您的問題,而不是在評論中丟失缺少的內容。 –

回答

2
echo -n "Enter your numbers: " 
read numbers 
total=$(echo "$numbers" | sed 's/ \+/ + /g' | bc) 
echo "The total is $total" 

示例使用:

Enter your numbers: 4 6 2.47 
The total is 12.47 

更多:在腳本中,這個問題是行total+=$(echo "${number}" | bc)。這解決了它:

#! /usr/bin/env bash 
total=0 
echo -n "How many number : "; read num 
for ((i=0; i<$num; i++)); 
do 
    echo -n "Enter your number : "; read number 
    total=$(echo "$total + ${number}" | bc) 
    echo "${total}" 
done