Quantcast
Viewing all articles
Browse latest Browse all 2

Function to get specified key from /proc/cpuinfo

On Linux, the file /proc/cpuinfo returns a set of key-value pairs, where the key and value are separated by a colon and each pair has its own line. It's a bit more complicated than that in reality, but for my needs that's as complicated as it needs to be. (I have only one processor core.)

My goal is to write a function in bash that takes the key for one of those items and returns the value corresponding to that key. Here's my current function:

function get_cpuinfo_prop () {
    TARGET_LINE=$(cat /proc/cpuinfo | grep ^$1)
    IFS=':'
    read -ra PARTS <<< "$TARGET_LINE"
    PROP_VALUE=${PARTS[1]}
    echo $PROP_VALUE
}

And you'd call it like this:

$(get_cpuinfo_prop 'key_name')

Is there a better way of doing it? How could I improve this code? Additionally, I think this might be returning extra whitespace before the actual value. How can I trim those characters off the beginning/end?


Viewing all articles
Browse latest Browse all 2

Trending Articles