0

I am new to shell script and I have started it today itself. Can anyone tell me how to store and retrieve the output of this command:

cat /proc/cpuinfo | grep "MHz" | awk '/cpu MHz/ {print $4}'

This command returns just four decimal numbers but I am struggling to store these four numbers. I have tried to store them in array but it has not been fruitful for me. Please help!!

6
  • it has not been fruitful for me - what does that mean? Please show what have you tried. Commented Feb 2, 2020 at 16:13
  • I have tried $speed=cat /proc/cpuinfo | grep "MHz" | awk '/cpu MHz/ {print $4}'. Then I try to print ${speed[0]}. It just prints all the four numbers seperated by space. But speed[1] returns nothing. Commented Feb 2, 2020 at 16:14
  • What's the point of the grep, when the awk behind it applies a more restrictive pattern? Commented Feb 2, 2020 at 17:02
  • Anyhow, yeah, you're assigning a regular string, not an array. When you try to access a string as if it were an array, it presents with all the data only in the first element. Commented Feb 2, 2020 at 17:03
  • readarray -t cpus < <(awk '/cpu MHz/ { print $4}' </proc/cpuinfo) will store all your results in the array named cpus; you can see them with declare -p cpus, or iterate over them with something like for cpu in "${cpus[@]}"; do echo "CPU speed: $cpu"; done Commented Feb 2, 2020 at 17:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.