为什么 'lspci --help |grep 驱动程序失败?

Why does `lspci --help | grep driver` fail?

提问人:CreeLoPer27 提问时间:8/30/2023 最后编辑:CyrusCreeLoPer27 更新时间:9/1/2023 访问量:56

问:

lspci --help | grep driver由于某种原因失败,它只是打印输出而不是被 grep 过滤。

如果我运行,我会得到:lspci --help | grep driver

lspci: invalid option -- '-'
Usage: lspci [<switches>]

Basic display modes:
-mm             Produce machine-readable output (single -m for an obsolete format)
-t              Show bus tree

Display options:
-v              Be verbose (-vv or -vvv for higher verbosity)
-k              Show kernel drivers handling each device
-x              Show hex-dump of the standard part of the config space
-xxx            Show hex-dump of the whole config space (dangerous; root only)
-xxxx           Show hex-dump of the 4096-byte extended config space (root only)
-b              Bus-centric view (addresses and IRQ's as seen by the bus)
-D              Always show domain numbers
-P              Display bridge path in addition to bus and device number
-PP             Display bus path in addition to bus and device number

Resolving of device ID's to names:
-n              Show numeric ID's
-nn             Show both textual and numeric ID's (names & numbers)
-q              Query the PCI ID database for unknown ID's via DNS
-qq             As above, but re-query locally cached entries
-Q              Query the PCI ID database for all ID's via DNS

Selection of devices:
-s [[[[<domain>]:]<bus>]:][<slot>][.[<func>]]   Show only devices in selected slots
-d [<vendor>]:[<device>][:<class>]              Show only devices with specified ID's

Other options:
-i <file>       Use specified ID database instead of /usr/share/hwdata/pci.ids
-p <file>       Look up kernel modules in a given file instead of default modules.pcimap
-M              Enable `bus mapping' mode (dangerous; root only)

PCI access options:
-A <method>     Use the specified PCI access method (see `-A help' for a list)
-O <par>=<val>  Set PCI access parameter (see `-O help' for a list)
-G              Enable PCI access debugging
-H <mode>       Use direct hardware access (<mode> = 1 or 2)
-F <file>       Read PCI configuration dump from a given file

而不是将输出传递给 。grep

我已经尝试过了,行为是一样的。zshbash

bash 终端 zsh io-重定向 stderr

评论

2赞 Cyrus 8/30/2023
许多命令将其错误消息写入 stderr (2) 而不是 stdout (1)。替换为 将 stderr 重定向到 stdout。|2>&1 |
1赞 user1934428 8/30/2023
从错误输出中可以看出,它没有提供名为 的选项。例如,如果您使用任何无意义选项运行它,它只是将一些解释性文本写入 stderr。这就是您看到此输出的原因。lspci--helplspci -j

答:

1赞 CreeLoPer27 8/30/2023 #1

通常,lsusb、lspci 和类似命令将其 --help 内容输出到 stderr,而不是像大多数命令那样输出到 stdout

一些像 lsusb 和 lspci 这样的实用程序没有 --help 选项,因为每当它们使用不正确时,它们都会打印出它们的帮助字符串(在这种情况下,帮助字符串被视为错误消息,这就是为什么它被输出到 而不是stderrstout)

为了解决这个问题,我故意使用我选择的随机字符串参数(我使用了“a”)使命令失败,并将 stderr 重定向到 stdout,如下所示:

lspci a 2>&1 | grep driver

感谢 Charles Duffy 的回答dschulz 评论和 Fravadona 评论的澄清和解释。

评论

3赞 dschulz 8/30/2023
中没有这样的选项。此外,它不支持任何你在 gnu 用户空间中通常看到的长 '--' 选项。--helplspcilspci
0赞 Charles Duffy 8/30/2023
@dschulz,呵呵,就是这样。添加一个答案。
1赞 Fravadona 8/30/2023
它就像您可以使用任何字符串作为参数来获取没有行的帮助消息一样:例如,lspci , .不过,它仍然犯了错误!/-.+/invalid optionlspci x-lspci help
0赞 CreeLoPer27 9/1/2023
@dschulz 确实你是对的,我在这里查看的是 setpci 的源代码,而不是 lspci:git.kernel.org/pub/scm/utils/pciutils/pciutils.git/tree/...,还查看了 lspci 源代码,实际上似乎没有“--help”选项,它只是在错误使用时打印出帮助字符串 git.kernel.org/pub/scm/utils/pciutils/pciutils.git/tree/......
3赞 2 revsCharles Duffy #2

错误消息本身会告诉您失败的原因:

lspci: invalid option -- '-'

--help不是一个有效的选项(其他任何以 开头的选项也不是 )。因此,你会得到一条写成诊断内容的使用消息,POSIX 指定诊断内容属于 stderr(它绕过管道 - 这通常是需要的,以确保诊断日志到达人工操作员而不是被管道使用)。--