Quantcast
Channel: Active questions tagged bash+awk - Ask Ubuntu
Viewing all articles
Browse latest Browse all 102

why is my output not correct while dividing by a variable in awk?

$
0
0

I wrote a small watch script to monitor the load average on a Linux server (just for learning purposes but watch uptime wasn't giving the output I want).

It works well, but to get the load average for each core, I divide the load average values by the number of cores. This is now accomplished by hardcoding the cores, which is inconvenient if I choose to use it on another server.

Due to that, I want to have the cores done by variable like nproc.

All is well and good, but for some reason, my output is completely different when I use nproc as the core count than when I hard code the cores.

This is my script now:

#! /bin/bashPATH=/usr/gnu/bin:/usr/bin:/usr/sbin:/sbin:/opt/csw/bin:/usr/local/bin:/binwhile truedo    uptime | awk '{ printf "%2.2f ",$(NF-2)/4 ; printf "%2.2f ",$(NF-1)/4  ; printf "%2.2f\n",$(NF)/4}'        sleep 1done

As you see I have divided the load average by 4 (4 core server). This gives me the following output:

$ ./watch-load.sh 1.05 0.96 1.091.05 0.96 1.090.96 0.94 1.080.96 0.94 1.080.96 0.94 1.08

but when I edit the script to use nproc it looks something like this:

#! /bin/bashPATH=/usr/gnu/bin:/usr/bin:/usr/sbin:/sbin:/opt/csw/bin:/usr/local/bin:/binwhile truedo    uptime | awk '{ printf "%.2f ",$10/$(nproc) ; printf "%2.2f ",$11/$(nproc)  ; printf "%2.2f\n",$12/$(nproc)}'        sleep 1done

This will give me the following results:

$ ./watch-load-test.sh 0.22 0.23 0.270.22 0.23 0.270.22 0.23 0.270.28 0.24 0.270.28 0.24 0.27

This is weird because nproc is showing 4 cores:

$ nproc4

I'm lost here. Do you have any suggestions why it doesn't seem to work the way it should?


Viewing all articles
Browse latest Browse all 102

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>