There is a weird behavior in Bash script. Trying to run following lines in two different systems.
Script:
cpuIdle=$(mpstat 5 1 | grep Average | awk '{ print $12 }')
cpuUsage=$(bc <<< "100.0-$cpuIdle")
And here are the details of machines.
System-1:
- Ubuntu 14.04.04 LTS
Linux 4.2.0-36-generic #42~14.04.1-Ubuntu SMP Fri May 13 17:27:22 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
awk --version
=GNU Awk 4.0.1
bash --version
=GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
System-2:
- Ubuntu 16.04.04 LTS
Linux 4.4.0-22-generic #40-Ubuntu SMP Thu May 12 22:03:46 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
awk --version
=GNU Awk 4.1.3, API: 1.1 (GNU MPFR 3.1.4, GNU MP 6.1.0)
bash --version
=GNU bash, version 4.3.42(1)-release (x86_64-pc-linux-gnu)
Script runs fluently on System-1 but gives following error on System-2 while running bc
:
(standard_in) 1: syntax error
I can confirm that in both systems cpuIdle vairable is being set correctly (1st line of the script).
I also can confirm that bc working without a problem in both systems when I set cpuIdle variable manually (like cpuIdle=97.3).
Can't decide if this is a bug or my mistake. Any suggestion or ideas?
It is the comma.
->
and
So this could be an internationalization issue (in Holland we use a comma for decimals). I would say: bug, someone forgot to include a comma as a possible decimal sign.
Switch to American annotation and it will work again.