Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of htop, but again, I need to assign the % load to a variable in a bash script.

Does anyone know how I can do it?

This will assign user cpu% used to variable c

This assumes, you only have 1 cpu and that you are only concerned with user cpu %

c=`iostat | awk '{if(NR==4) {print $1}}'`

My distro doesn't have iostat :slight_smile: I need to figure out which package contains it.

sysstat