0

top -n 2 -d 5 | grep Cpu | awk 'NR==2' | sed -e "s/\%//g" | sed -e "s/\us,//g" > temp

cpu_value=awk '{print $2}' temp

cpu_calc=echo "scale=4; 100 - $cpu_value" | bc

I am getting the below error:

(standard_in) 1: illegal character: ^[ (standard_in) 1: parse error

Suren
  • 37
  • 7
  • What does scale to bc? if you just do `echo "scale=4; 100 - $cpu_value"` the result is what bc has to process and bc is who is generating the error. on my laptop it produced `scale=4; 100 - 6,8` I think you have to replace the ',' with a '.' – Falk Sep 11 '14 at 10:12

1 Answers1

0

Just Add tr to replace the , with a .

cpu_calc=`echo "scale=4; 100 - $cpu_value" | tr , . | bc`

It seems to work on my laptop.

Edit: As you should know the decimal separator and the thousands separators are different on different countries, and therefore can also be different if some commands have country/language specific number formatting. In Spain we write 42.571,93 in other countries you can see 42,571.93 for computers however the correct form is 42571.93 (this number is just an example).

Falk
  • 469
  • 1
  • 7
  • 19
  • Thanks n Sorry this is not working. Its working when I am trying the below, I hope the problem is with the variable bash-3.2$ echo "scale=4; 100 - 6.6" | bc 93.4 – Suren Sep 11 '14 at 11:23
  • Is the output message still the same? – Falk Sep 14 '14 at 14:39
  • You can use `LC_ALL=C` before the command to force C-style locale, e. g. without any local quirks and (of course) overriding any locale settings. – syntaxerror Jun 29 '15 at 18:43