替代使用udevadm命令

时间:2016-06-21 14:36:57

标签: linux usb udev

我有一个使用命令udevadm来获取有关/ dev设备的信息的脚本,我的脚本所需的信息是ID_SERIAL值,因为我想过滤tty's表示连接到USB端口的特定产品的设备。问题是,环境中存在的udevm的udevadm版本为128,此版本的udevadm没有查询property可用。如何以其他方式过滤usb端口上连接的设备?

编辑1 的 Command.sh

#!/bin/bash

for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev);do
    (syspath="${sysdevpath%/dev}";
     echo "syspath = $syspath";
     devname="$(udevadm info -q name -p $syspath)";
     [[ "$devname" == "bus/"* ]]&& continue;
     echo "Passou";
     eval "$(udevadm info -q property --export -p $syspath)";
     [[ -z "$ID_SERIAL" ]] && continue;
     temporario=${ID_SERIAL,,};
     if [[ $temporario == *"ManufacturerName"* ]];
     then 
        echo "/dev/$devname";
     fi);
done;

1 个答案:

答案 0 :(得分:0)

使用dmesg的输出不是更容易吗?当我插入我的串口时,我得到了这个输出:

[332144.226566] usb 1-2.1.2: new full-speed USB device number 6 using ehci-pci
[332144.302766] usb 1-2.1.2: New USB device found, idVendor=067b, idProduct=2303
[332144.302770] usb 1-2.1.2: New USB device strings: Mfr=0, Product=0, SerialNumber=0
[332144.512664] usbcore: registered new interface driver usbserial
[332144.512679] usbcore: registered new interface driver usbserial_generic
[332144.512691] usbserial: USB Serial support registered for generic
[332144.522042] usbcore: registered new interface driver pl2303
[332144.522060] usbserial: USB Serial support registered for pl2303
[332144.522090] pl2303 1-2.1.2:1.0: pl2303 converter detected
[332144.523681] usb 1-2.1.2: pl2303 converter now attached to ttyUSB0

最后一行提及ttyUSB0,如果您想识别remove-event,也可以将其与1-2.1.2相关联:

[332342.342682] usb 1-2.1.2: USB disconnect, device number 6
[332342.342891] pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
[332342.342903] pl2303 1-2.1.2:1.0: device disconnected

另一种可能性是使用inotify系统,您可以在插入驱动器时收到通知。请检查this article on how to use the system