I wrote a script for my dwm statusbar. One part of it is finding the current cpu usage for every single core on my system. I figured out a way myself but I would need some help fixing a bug in this. Here is the command: top -bn 2 -d 0.5 | grep -F '%Cpu' | tail -n 4 | awk '{print $3}'. This assumes that your top shows every core per default. You can achieve this by issuing top then pressing 1 and then pressing W to save the current configuration to a .toprc file in your home folder. Everytime you open top now it will display all of your cores. My aforementioned command has the drawback that when assigned to a variable like this:CPU=$(top -bn 2 -d 0.5 | grep -F '%Cpu' | tail -n 4 | awk '{print $3}')and then using $CPU with xsetroot like this: xsetroot -name $CPU I will get the output I want in my statusbar but between every cpu percentage there will be two symbols on top of each other separating them v and t. How do I get rid of them? Has this something to do that I might be using an array instead of a string here?You can see the problem on the left side of the picture.Command:top -bn 2 -d 0.5 | grep -F '%Cpu' | tail -n 4 | awk '{print $3}'sample output:14.312.08.010.0Note for anyone using this script: When the cpu usage for a core reaches 100% the array that the command outputs will move the column with the current load from column 3 to column 2. Hence, with awk '{print $3}' you will then see us, as output for column 3. If you're fine with that leave it. If not you could have awk print column 2 as well. It will just be :. A solution that avoids all those pitfalls is:top -bn 2 | grep -F '%Cpu' | tail -n 4 | gawk '{print $2 $3}' | tr -s '\n\:\,[:alpha:]' ' '
How to print a bash variable that has an array as value
bash;shell script;xorg;x11;dwm
The stuff between the values seems a visual representation of the newline character to me ( octal character code 12), which you would get when using:echo -e 'a\012b'What you could try is pipe the output through tr '\n' ' ' as with:echo -e 'a\012b' | tr '\n' ' '