1

I'm trying to do the following:

size=$(curl -sI "https://stackoverflow.com/posts/0/editor-heartbeat/ask" | grep -i Content-Length | awk '{print $2}')
echo $((size / 3))

But this returns the error:

")syntax error: invalid arithmetic operator (error token is "

I guess it has something to do with the result of awk.

Thank you in advance.

Evanusso
  • 129
  • 1
  • 10
  • Possible duplicate of [How can I do division with variables in a Linux shell?](https://stackoverflow.com/questions/18093871/how-can-i-do-division-with-variables-in-a-linux-shell) – Jonathan Hall Nov 28 '18 at 15:59
  • Why not do the arithmetic in `awk` at the end of the pipe? – dawg Nov 28 '18 at 16:09
  • 1
    `curl -sI "https://stackoverflow.com/posts/0/editor-heartbeat/ask" | awk '/Content-Length/{print $2/3; exit}'` – kvantour Nov 28 '18 at 16:21
  • and an `echo "size=${size}ZZZ"` before your calculation to be sure there is only numeric values saved to `$size`. Good luck. – shellter Nov 28 '18 at 16:28

2 Answers2

5

Remove the carriage return \r from the size:

size=${size%$'\r'}
choroba
  • 231,213
  • 25
  • 204
  • 289
1

Here is a useful function for division I picked previously on another thread:

calc() {
awk "BEGIN { print "$*" }";
}

End result:

calc() {
awk "BEGIN { print "$*" }";
}
size=$(curl -sI "https://stackoverflow.com/posts/0/editor-heartbeat/ask" | grep -i Content-Length | awk '{print $2}')
echo "$(calc ${size}/3)"

Execution:

ivo@spain-nuc-03:~/Downloads/TestStackoverflow$ ./processing.sh 
18758.3
ivo@spain-nuc-03:~/Downloads/TestStackoverflow$ 

Hope it helps!

Ivo Yordanov
  • 146
  • 1
  • 8