I’m trying to get the CPU frequency in MHz to include it in Conky.
The command I’m using is: cat /proc/cpuinfo | grep "^[c]pu MHz
cpu MHz : 3593.772
cpu MHz : 3602.633
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 3568.188
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 3596.717
cpu MHz : 550.000
cpu MHz : 3592.460
cpu MHz : 550.000
I’m trying to get one line per time to include in my conky.conf… Something like this:
Core0: 3593.772 MHz
Core1: 3602.633 MHz
I’m currently stuck with this cat /proc/cpuinfo | grep "^[c]pu MHz" | head -1 | cut -c12-19
This shows the first line, how can I get the second line, third and etc?
$ cat /proc/cpuinfo | grep "^[c]pu MHz" | head -18
cpu MHz : 3590.813
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 3591.224
cpu MHz : 550.000
cpu MHz : 3589.665
cpu MHz : 3599.029
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 550.000
cpu MHz : 3590.804
cpu MHz : 550.000
cpu MHz : 550.000
is an anti-pattern called Useless Use of Cat. You should always avoid it. The grep utility can take a file name, so you should use this instead:
grep 'pattern' "file"
Also, notice that the 'pattern' argument is in single quotes, while the "file" argument is in double quotes. This is because we never want shell globbing in the pattern argument, but often we do want it in the filename argument.
Rarely, a utility like grep does not accept a filename as an argument, and it only works with stdin. Not even in this case should you use cat to read the file, because the shell has input/output redirection for that:
command < "file"
Probably the most useless use of cat is for reading a file and saving it into a variable, like this:
variable=$(cat "file")
Here, shell input redirection also works better:
variable=$(<"file")
In general, it is best practice to completely forget that the cat utility prints the file to stdout. That is not its purpose and it shouldn’t be used that way (though, in an interactive shell session, there is no harm, except to one’s self-esteem).
There is always a better way to read a file than using cat. Instead, cat should only be used to concatenate two or more files into one. When you have only one file, it is a completely useless tool.