bash Mac OSX 获取 USB 供应商 ID 和产品 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22042661/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:42:00  来源:igfitidea点击:

Mac OSX get USB vendor id and product id

macosbashusb

提问by Sceik

I'm a newbie in Mac OSX world and I have to write a script which gives me the vendor id and product id of a connected usb device. I have done it for Windows and Linux but for Mac I have no idea where to start from.

我是 Mac OSX 世界的新手,我必须编写一个脚本,该脚本为我提供连接的 USB 设备的供应商 ID 和产品 ID。我已经为 Windows 和 Linux 完成了它,但对于 Mac 我不知道从哪里开始。

I have seen this postbut the link with the example is not working. Do you guys have any advice about where I can start from or where I can find some examples?

我看过这篇文章,但与示例的链接不起作用。你们对我可以从哪里开始或在哪里可以找到一些例子有什么建议吗?

In particular, which language should I use?

特别是,我应该使用哪种语言?

回答by ghoti

You tagged your question with bash, so I'll answer it as if you're asking how to do this in bash, rather than asking what language to use (which would make the question off-topic for StackOverflow).

你用 标记了你的问题bash,所以我会回答它,就好像你在问如何在 bash 中做到这一点,而不是问使用什么语言(这会使问题偏离 StackOverflow 的主题)。

You can parse existing data from system_profiler using built-in tools. For example, here's a dump of vendor:product pairs, with "Location ID" and manufacturer...

您可以使用内置工具解析来自 system_profiler 的现有数据。例如,这里是 vendor:product 对的转储,带有“位置 ID”和制造商...

#!/bin/bash

shopt -s extglob

while IFS=: read key value; do
  key="${key##+( )}"
  value="${value##+( )}"
  case "$key" in
    "Product ID")
        p="${value% *}"
        ;;
    "Vendor ID")
        v="${value%% *}"
        ;;
    "Manufacturer")
        m="${value}"
        ;;
    "Location ID")
        l="${value}"
        printf "%s:%s %s (%s)\n" "$v" "$p" "$l" "$m"
        ;;
  esac
done < <( system_profiler SPUSBDataType )

This relies on the fact that Location IDis the last item listed for each USB device, which I haven't verified conclusively. (It just appears that way for me.)

这依赖于Location ID为每个 USB 设备列出的最后一项,我尚未最终验证。(对我来说就是这样。)

If you want something that (1) is easier to read and (2) doesn't depend on bash and is therefore more portable (not an issue though; all Macs come with bash), you might want to consider doing your heavy lifting in awk instead of pure bash:

如果您想要 (1) 更容易阅读并且 (2) 不依赖于 bash 并且因此更便携(虽然不是问题;所有 Mac 都带有 bash),您可能需要考虑在awk 而不是纯 bash:

#!/bin/sh

system_profiler SPUSBDataType \
    | awk '
      /Product ID:/{p=}
      /Vendor ID:/{v=}
      /Manufacturer:/{sub(/.*: /,""); m=
#!/usr/bin/awk -f

BEGIN {

  while ("system_profiler SPUSBDataType" | getline) {
    if (/Product ID:/)   {p=}
    if (/Vendor ID:/)    {v=}
    if (/Manufacturer:/) {sub(/.*: /,""); m=
$ system_profiler -xml SPUSBDataType
} if (/Location ID:/) {sub(/.*: /,""); printf("%s:%s %s (%s)\n", v, p,
system_profiler -detailLevel full
, m)} } }
} /Location ID:/{sub(/.*: /,""); printf("%s:%s %s (%s)\n", v, p,
  | |   |   | +-o FaceTime HD Camera (Built-in)@0  <class IOUSBInterface, id 0x1000002b2, registered, matched, active, busy 0 (26 ms), retain 7>

| |   |   | |   {
| |   |   | |     "IOCFPlugInTypes" = {"2d9786c6-9ef3-11d4-ad51-000a27052861"="IOUSBFamily.kext/Contents/PlugIns/IOUSBLib.bundle"}
| |   |   | |     "bcdDevice" = 0x755
| |   |   | |     "IOUserClientClass" = "IOUSBInterfaceUserClientV3"
| |   |   | |     "idProduct" = 0x850b
| |   |   | |     "bConfigurationValue" = 0x1
| |   |   | |     "bInterfaceSubClass" = 0x1
| |   |   | |     "locationID" = 0xfffffffffa200000
| |   |   | |     "USB Interface Name" = "FaceTime HD Camera (Built-in)"
| |   |   | |     "idVendor" = 0x5ac
, m);} '

Or even avoid wrapping this in shell entirely with:

或者甚至避免将其完全包装在 shell 中:

find /dev -type b -o -type c

Note that you can also get output from system_profilerin XML format:

请注意,您还可以从system_profilerXML 格式获取输出:

##代码##

You'll need an XML parser to handle that output, though. And you'll find that it's a lot of work to parse XML in native bash.

不过,您将需要一个 XML 解析器来处理该输出。您会发现在原生 bash 中解析 XML 需要做很多工作。

回答by Mark Setchell

Depending on what you want to do with the information, you can just look it up in System Information.

根据您想对信息做什么,您可以在System Information.

Click the Applemenu at top left of screen, About this Mac, More Info, System Reportand select Hardwareat top left, then USB.

单击Apple屏幕左上角的菜单、About this MacMore InfoSystem Report然后选择Hardware左上角的 ,然后USB

You could maybe write Applescript to do that, but if you are going to go on and interact with the device in some way, this may not be the best approach.

您也许可以编写 Applescript 来做到这一点,但如果您要继续以某种方式与设备交互,这可能不是最好的方法。

回答by Mark Setchell

You can use the system_profiler command, like this:

您可以使用 system_profiler 命令,如下所示:

##代码##

and parse the outout from that. Or you can add the -xml option to the system_profiler command and parse XML pretty easily with awk/grep or the XML module in Perl.

并从中解析输出。或者您可以将 -xml 选项添加到 system_profiler 命令并使用 awk/grep 或 Perl 中的 XML 模块非常轻松地解析 XML。

Example extract:

示例摘录:

##代码##

Regarding the path to the USB device, I have no idea how you would do that simply on a Mac. I might be tempted to run:

关于 USB 设备的路径,我不知道你会如何在 Mac 上做到这一点。我可能会想跑:

##代码##

before inserting the USB device, and saving the output. Then have your user insert the device and run the same command again to see what device special files have been added as a result of plugging in your device. Maybe crude, maybe effective - just an idea.

在插入 USB 设备之前,并保存输出。然后让您的用户插入设备并再次运行相同的命令以查看由于插入设备而添加了哪些设备特殊文件。也许粗糙,也许有效——只是一个想法。