1

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?

  • You might want to look at mpstat's output. – muru Jun 6 '16 at 21:59
  • Already looked it. I did not see any differences. – benjamin button Jun 6 '16 at 22:00
  • it is the comma so probably a language issue? – Rinzwind Jun 6 '16 at 22:03
  • Are you sure? I'd bet that cpuIdle is empty or an invalid number. Tried echoing cpuIdle? – muru Jun 6 '16 at 22:03
  • @muru thx for the comment but Rinzwid found the problem. I was editing my question according to your suggestions before I got the answer. – benjamin button Jun 6 '16 at 22:10
2

It is the comma.

$ cpuIdle=$(mpstat 5 1 | grep Average | awk '{  print $12 }')
$ echo $cpuIdle
99,25

->

$ cpuUsage=$(bc <<< "100.0-95,25")
(standard_in) 1: syntax error

and

$ cpuUsage=$(bc <<< "100.0-95.25")
$ echo $cpuUsage
4.75

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.

  • Yep that's correct. It was comma. It must be bug as you suggested. – benjamin button Jun 6 '16 at 22:12
  • @bensen not a bug (at least as far as the devs are concerned). It's in the manpage: LANG environment This version does not conform to the POSIX standard in the processing of the LANG environment variable and all environment variables starting with LC_. – muru Jun 6 '16 at 22:14
  • do not know. actually I've been applying same settings to my fresh installs since 12.10. living in Turkey but never set anything according to country. strange.. do you have any idea how can I change annotation settings? – benjamin button Jun 6 '16 at 22:15
  • 1
    @muru ehm. "This version does not conform to the POSIX standard" That I would consider a bug >:-D – Rinzwind Jun 6 '16 at 22:15
  • 1
    @bensen see manpages.ubuntu.com/manpages/trusty/en/man1/bc.1.html (section DIFFERENCES). – muru Jun 6 '16 at 22:22

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.