From top, I get the required value of the CPU utilization by the process, it is issued in the form of 10.0 (maybe 1.6, for example) and maybe more or less. It is necessary to compare this value with the threshold (static) and in the case of exceeding the threshold to perform the action.

Through bc, something does not come out, but through it you can successfully compare numbers like 10.0. So the option of replacing "," with ".". How to do it?

    3 answers 3

    The top program is normally localized, and therefore, for the correct display of the so-called. The decimal point uses the current locale.

    you most likely use the ru_RU.UTF-8 locale.

    You can see the value of the “decimal point” for any locale like this:

     $ LC_ALL=имя-локали locale -c decimal_point 

    for example, for ru_RU.UTF-8 it will be a comma:

     $ LC_ALL=ru_RU.UTF-8 locale -c decimal_point LC_NUMERIC , 

    and for a special C locale (also known as the default, “posix-ovish” locale ), a full stop:

     $ LC_ALL=C locale -c decimal_point LC_NUMERIC . 

    summary:

    if you run the top program with the C locale, i.e., it’s not easy:

     $ top опции-и-параметры 

    but

     $ LC_ALL=C top опции-и-параметры 

    you will get in the output what you need - a point as the separator of the integer and fractional parts:

     $ LC_ALL=C top -bn1 -p1 | tail -n 1 1 root 20 0 2300 744 640 S 0.0 0.0 0:27.46 init 
    • It makes sense to do so always, when parsing the output of a command, some commands in other locales may unexpectedly output data in a completely different way. - sercxjo
    • Thank you, I liked the decision, did just that. - Sebastian Pereiro

    Bash can do it himself:

     $ a="10,32" $ echo ${a/,/.} 10.32 
    • one
      and to replace all occurrences , you need to add one more slash: ${a//,/.} - aleksandr barakin

    I guess you can try using sed 's/foo/bar/' - replace foo with bar, in your case write sed 's/,/./'

    • can sed read variables? I do not want to produce files / logs - Sebastian Pereiro
    • As an example, HER="10,0"; echo $HER |sed 's/,/./' HER="10,0"; echo $HER |sed 's/,/./' - nobody
    • Thank you, it went right with HER ^^ - Sebastian Pereiro