我正在尝试一些非常简单的事情,但我正在尝试的所有代码都不起作用。 我需要在bash中添加两个浮点数。我这样做:
result1=`$CURL -o /dev/null -s -w %{time_total} $url1`
result2=`$CURL -o /dev/null -s -w %{time_total} $url2`
result3=`$CURL -o /dev/null -s -w %{time_total} $url3`
total= `expr $result2 + $result3`
echo $total | $GAWK -F: '{ print "connection_1.value " $1 }'
但是在提示符中我得到了这个输出:
./http_response_2: line 12: 0,018+0,255: command not found
connection_1.value
我也在尝试这样做:
result1=`$CURL -o /dev/null -s -w %{time_total} $url1`
result2=`$CURL -o /dev/null -s -w %{time_total} $url2`
result3=`$CURL -o /dev/null -s -w %{time_total} $url3`
total= `$result2 + $result3 | bc`
获得相同的结果。 提前谢谢!
答案 0 :(得分:3)
有3个问题:
total=
&之间不应有空格。 `$result2 + $result3
解决所有这些问题:
total=$(tr ',' '.' <<< "$result2 + $result3" | bc -l)
如果您担心小数点前的前导0,请尝试:
total=$(tr ',' '.' <<< "$result2 + $result3" | bc -l | xargs printf "%g")
答案 1 :(得分:0)
不要用点替换逗号,不要在第一时间生成逗号。
它们来自本地化,因此使用LC_ALL = C作为前缀,如:
LC_ALL=C curl -o /dev/null -s -w %{time_total} www.google.com
并放弃过时的反叛,改为使用$(...):
result1=$(LC_ALL=C $CURL -o /dev/null -s -w %{time_total} $url1)