lsusb
输出连接设备的 USB 总线和 USB 端口。在输出中,一些 USB 根集线器是内部 USB 集线器,还连接了蓝牙和网络摄像头等,请参阅https://unix.stackexchange.com/questions/144029/command-to-determine-ports-of- a-device-like-dev-tty-usb0
您应该弄清楚您的外部端口连接在哪个 USB 总线上。在我的电脑上,所有外部 USB 端口都链接到Bus 01
为此,请检查 的输出lsusb -t
,然后连接 USB 设备并lsusb -t
再次检查 的输出。那么您知道您的三个外部 USB 端口在您的设备内部 USB 结构树中的“地址”:
内部 USB 端口:
/: Bus 04.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/5p, 12M
/: Bus 03.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/5p, 12M
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/5p, 480M
|__ Port 1: Dev 64, If 0, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M
|__ Port 1: Dev 64, If 1, Class='bInterfaceClass 0x0e not yet handled', Driver=uvcvideo, 480M
外部 USB 端口:
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/5p, 480M
|__ Port 3: Dev 116, If 0, Class='bInterfaceClass 0xe0 not yet handled', Driver=rndis_host, 480M
|__ Port 3: Dev 116, If 1, Class=data, Driver=rndis_host, 480M
连接到外部端口 #2 的 USB 记忆棒
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/5p, 480M
|__ Port 2: Dev 119, If 0, Class=stor., Driver=usb-storage, 480M
|__ Port 3: Dev 116, If 0, Class='bInterfaceClass 0xe0 not yet handled', Driver=rndis_host, 480M
|__ Port 3: Dev 116, If 1, Class=data, Driver=rndis_host, 480M
在此过程之后,您将获得外部 USB 端口的“地址”
在dmesg
连接的 USB 设备中,总是出现一行包含 USB 总线和端口号:
[186067.360139] usb 1-1: new high-speed USB device number 123 using ehci_hcd
是总线 001 端口 001
[186067.360139] usb 1-2: new high-speed USB device number 123 using ehci_hcd
是总线 001 端口 002
[186067.360139] usb 1-3: new high-speed USB device number 123 using ehci_hcd
是总线 001 端口 003
在您的脚本中,您使用命令 grep 这一行dmesg | grep "usb 1" | tail -1
(尾部 -1 表示最后一次出现,请参阅http://www.stackoverflow.com/questions/24014194/how-to-grep-the-last-occurence-of-the -线型)
可以直接使用命令获取端口号
dmesg | grep -o -P 'usb 1.{0,3}' | tail -1 | head -c 7 | tail -c 1
(如果您所有的外部端口都打开Bus 001
)
(匹配前后的 Grep 字符?,http://www.unix.com/unix-for-dummies-questions-and-answers/28542-how-do-i-get-nth-character-string.html)
因此,您可以获得最新的 USB 设备(您的设备)所连接的 USB 端口号,您可以在udev
脚本中使用它(if ...
)
您还可以在 /dev/bus/usb/
文件系统中找到 USB 总线树结构,即 Bus 01 Port 1 是/dev/bus/usb/001/001
见http://www.linuxnix.com/find-usb-device-details-in-linuxunix-using-lsusb-command/