0

I'd like to have video card information from my target system, no matter what it is. There are two lines returned from my current target system, and I'd like to treat each line as an element of array. Using the code below, I got every word from lspci result rather than whole line which is what I need. Any idea?

myvideos=(`lspci | grep VGA`)
for video in ${myvideos[@]}
do
   echo "The $video"
done

The result returned from the code are:

The 00:02.0
The VGA
The compatible
The controller:
The Intel
The Corporation ....

What I need is :

00:02.0 VGA compatible controller: Intel Corporation

Thank you!

John1024
  • 109,961
  • 14
  • 137
  • 171
Tao Wang
  • 186
  • 1
  • 18

2 Answers2

3
  1. use mapfile to capture the output into an array.

    mapfile -t myvideos < <(lspci | grep VGA)
    
  2. Absolutely crucial to use quotes on the array in the for loop

    for video in "${myvideos[@]}"; do ...
    
Community
  • 1
  • 1
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

There are other ways to do the same without using arrays:

lspci | grep VGA |  xargs -i echo The {}

or

lspci | grep VGA | awk '{print "The " $0}'
Vytenis Bivainis
  • 2,308
  • 21
  • 28