linux - 如何读取每行命令 lspci 的结果作为数组的一个元素?

标签 linux bash shell

我想从我的目标系统中获取视频卡信息,无论它是什么。我当前的目标系统返回了两行,我想将每一行视为数组的一个元素。 使用下面的代码,我从 lspci 结果中得到了每个单词,而不是我需要的整行。有什么想法吗?

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

代码返回的结果是:

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

我需要的是:

00:02.0 VGA compatible controller: Intel Corporation

谢谢!

最佳答案

  1. 使用 mapfile将输出捕获到数组中。

    mapfile -t myvideos < <(lspci | grep VGA)
    
  2. use quotes on the array 绝对重要在for循环中

    for video in "${myvideos[@]}"; do ...
    

关于linux - 如何读取每行命令 lspci 的结果作为数组的一个元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33530377/

相关文章:

Python Flask Socket 报错(Linux 环境新手)

Java ProcessBuilder 无法运行简单的脚本

linux - 从源代码安装后如何卸载 libc++?

linux - Linux 上的 Qt5 - Linguist 在哪里?

linux - 如何在匹配模式后立即将字符串路径插入文件行的中间?

windows - 文件重定向中出现杂散换行符

python - 如何在 shell 中 su 并运行 python 脚本?

c - 发生错误时如何在makefile中同时退出并记录文件中的数据

bash - 防止在 heredocs 中计算反引号中的表达式

Linux如何在脚本中查找所有可执行文件