zsh system status script
Extracting a column

It's a lot easier to extract columns from text using awk.
Instead of this:


grep "cpu MHz" /proc/cpuinfo | sed 's/[\t ]*//g; s/cpuMHz://g'



This is shorter, and uses a single awk process instead of grep + sed:

awk '/cpu MHz/ {print $4}' /proc/cpuinfo


Extracting a line

This is a really awkward method to extract the 4th line:


echo $cpu_freq | head -n 4 | tail -n 1



This is simpler and better:

sed -ne 4p &lt;&lt;&lt; "$cpu_freq"


Using arrays

Instead of getting the values of cpu0..cpu3 one by one by running separate command(s) for each, you can get it in one go:

cpus=($({ echo scale=2; awk '/cpu MHz/ {print $4 " / 1000"}' /proc/cpuinfo; } | bc))


Explanation:


The awk command appends the " / 1000" to the CPU frequency, so it's ready for piping to bc
Pipe all lines (commands) to bc at once, and get all the output at once
Group the echo scale=2 and the awk for bc by enclosing in { ...; }
Store the output of bc in an array


From this array, you can extract the individual CPU values

cpu0=${cpus[0]}
cpu1=${cpus[1]}
cpu2=${cpus[2]}
cpu3=${cpus[3]}


Next steps

I don't want to spoil all the fun for you :-)
Following the techniques above,
you should be able to extract the other values more efficiently (using fewer processes) in a similar fashion.