ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Here is the part of the larger script; I'm trying to get cpu idle time
using top command, then grepping for one of the top lines, the one with
cpu idle statistic, then field 4 with gawk and then getting rid of the percentage sign, blanks spaces etc (tr). This gets me the string I want ok but it seems that the string array has many blank elements.
So for example after i execute first line below and assuming cpu idle time is 90% i would get cpu_free equal to "90". However when i try to loop using for loop below i get many more elements than just two i should have. The 0 lement is "90" however. Any idea what I'm doing wrong here?
cpu_free=`top -n 1 | grep -i cpu\(s\)| gawk -F, '{print $4}' | tr -d "%,id. "`
echo $cpu_free
for ((i=0;i<=35;i++))
do
echo "element $i is |${cpu_free[$i]}|"
done
while true; do top -n 1 | grep -i cpu\(s\) | gawk -F, '{print $4}' | sed 's/%.*//'; done
-twantrd
shorter version
Code:
while ...top -n 1 | awk -F, '/Cpu\(s\)/{sub(/%.*/,"",$4);print $4}' ..done
the various options of top command may (or may not) indicate a way to only show the Cpu(s) line and field $4, so OP can read up on that if (s)he wants to.
the only reason i used a for loop is to actually check that i have those extra array
elements. when i do string length test it shows that string is 36 characters long
even though only 2 numbers show up when i print it. That's why i constructed the for loop
to verify that there are actually those empty elements there.
I know that decimal point is removed I will fix this later for now i need to figure out
the problem above.
thanks
Why check for the string length when you are displaying output from a certain field already?? That field remains constant and won't change. I don't understand your reasoning for a for loop in this one.
the reason for me trying a loop is just to see if the string length command actually
output the correct answer. And it does. Line 1 in the script produces string array which
is 36 elements long. for loop confirms it. I'm not sure how to get rid of the empty
elements.
Basically how would you guys go about getting idle cpu percentage. Maybe I'm going about this in
a completely wrong way.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.