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!!

Vipul Tyagi
  • 547
  • 3
  • 11
  • 29
  • `it has not been fruitful for me` - what does that mean? Please show what have you tried. – KamilCuk Feb 02 '20 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. – Vipul Tyagi Feb 02 '20 at 16:14
  • What's the point of the grep, when the awk behind it applies a more restrictive pattern? – Charles Duffy Feb 02 '20 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. – Charles Duffy Feb 02 '20 at 17:03
  • `readarray -t cpus < <(awk '/cpu MHz/ { print $4}' – Charles Duffy Feb 02 '20 at 17:15
  • (that code's a lot more efficient, too; no reason to run all three of `cat`,`grep` and `awk`, all three of which are separate executables rather than built-in components of the shell, when one could do with only `awk`). – Charles Duffy Feb 02 '20 at 17:16

0 Answers0