0

I'm trying something very easy but all code that I'm trying doesnt work. I need add two float numbers in bash. I'm doing this:

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 }'

but in the prompt I'm getting this output:

./http_response_2: line 12: 0,018+0,255: command not found
connection_1.value

I'm trying too do this:

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`

getting the same result. Thanks in advance!

hcarrasko
  • 2,320
  • 6
  • 33
  • 45

2 Answers2

3

There are 3 issues:

  1. There should be no space between total= & `
  2. echo missing before $result2 + $result3
  3. There is comma in your input, instead of decimal point.

Fixing all these issues:

total=$(tr ',' '.' <<< "$result2 + $result3" | bc -l) 

If you are concerned about the leading 0 before decimal point, try:

total=$(tr ',' '.' <<< "$result2 + $result3" | bc -l | xargs printf "%g") 
anishsane
  • 20,270
  • 5
  • 40
  • 73
  • thank you very much master :) `total=$(tr ',' '.' <<< "$result2 + $result3" | bc -l | xargs printf "%g")` solves my problem – hcarrasko Apr 28 '14 at 14:26
0

Instead of replacing commas with dots, don't produce commas in the first place.

They emerge from localization, so use LC_ALL=C as prefix, like:

LC_ALL=C curl -o /dev/null -s -w %{time_total} www.google.com 

and abandon the outdated backticks, use $(...) instead:

result1=$(LC_ALL=C $CURL -o /dev/null -s -w %{time_total} $url1)
user unknown
  • 35,537
  • 11
  • 75
  • 121