I want to store each output of commands mpstat
or mpstat -P ALL
, like %usr,%nice,%sys,...
in a variable.
Can you help me to do this,because I want to sum some of them.
Its a tough one but still I am managed to get something for you.
This line will print CPU data in a variable named CPU
eval "$(mpstat | awk '{print $3}' | xargs | awk '{print $2"="$3}')"
after doing this execute echo $CPU
you will get
[root@localhost BASH]# echo $CPU
all
[root@localhost BASH]#
and for %usr
you can use eval "$(mpstat | awk '{print $4}' | xargs | awk '{print "USR="$3}')"
look at below example
[root@localhost BASH]# eval "$(mpstat | awk '{print $4}' | xargs | awk '{print "USR="$3}')"
[root@localhost BASH]# echo $USR
0.11
[root@localhost BASH]#
I hope you understand how to do this and you can develop complete code for what you want.
Summary:
[root@localhost BASH]# cat forsome.sh
#!/bin/bash
eval "$(mpstat | awk '{print $3}' | xargs | awk '{print $2"="$3}')"
eval "$(mpstat | awk '{print $4}' | xargs | awk '{print "USR="$3}')"
echo "Number of CPU's :"$CPU
echo "%usr value :"$USR
[root@localhost BASH]# ./forsome.sh
Number of CPU's :all
%usr value :0.11
[root@localhost BASH]#