如何查找在Linux(Ubuntu)中连接的所有USB设备?

时间:2016-01-24 08:43:12

标签: linux bash shell

我在某个地方找到了一个脚本,它部分完成了我想要的:

#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
    (
        syspath="${sysdevpath%/dev}"
        devname="$(udevadm info -q name -p $syspath)"
        [[ "$devname" == "bus/"* ]] && continue
        eval "$(udevadm info -q property --export -p $syspath)"
        [[ -z "$ID_SERIAL" ]] && continue
        echo "{'name': '/dev/$devname', 'id_serial': '$ID_SERIAL'}"
    )
done

但这样我就可以获得所有连接的设备:

{'name': '/dev/ttyACM3', 'id_serial': 'Arduino__www.arduino.cc__0043_74132343221092D0'}
{'name': '/dev/input/event16', 'id_serial': 'Logitech_USB_Receiver'}
{'name': '/dev/input/mouse2', 'id_serial': 'Logitech_USB_Receiver'}
{'name': '/dev/hidraw0', 'id_serial': 'Logitech_USB_Receiver'}
{'name': '/dev/usb/hiddev0', 'id_serial': 'Logitech_USB_Receiver'}
{'name': '/dev/input/event17', 'id_serial': 'Logitech_USB_Receiver'}
{'name': '/dev/hidraw1', 'id_serial': 'Logitech_USB_Receiver'}
{'name': '/dev/ttyACM0', 'id_serial': 'Lenovo_N5321_gw_41C01111111A76M0'}
{'name': '/dev/ttyACM1', 'id_serial': 'Lenovo_N5321_gw_41C0BAAAAD5A76M0'}
{'name': '/dev/ttyACM2', 'id_serial': 'Lenovo_N5321_gw_41C0B11111111110'}
{'name': '/dev/input/event15', 'id_serial': 'SunplusIT_INC._Integrated_Camera'}
{'name': '/dev/media0', 'id_serial': 'SunplusIT_INC._Integrated_Camera'}
{'name': '/dev/video0', 'id_serial': 'SunplusIT_INC._Integrated_Camera'}

如何只获取/dev/tty*个(不一定使用相同的代码)?

1 个答案:

答案 0 :(得分:1)

#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
    syspath="${sysdevpath%/dev}"
    devname="$(udevadm info -q name -p $syspath)"
    [[ "$devname" == "bus/"* ]] && continue
    [[ "$devname" != "tty"* ]] && continue

    eval "$(udevadm info -q property --export -p $syspath)"
    [[ -z "$ID_SERIAL" ]] && continue
    echo "{'name': '/dev/$devname', 'id_serial': '$ID_SERIAL'}"
done

这一行:

[[ "$devname" != "tty"* ]] && continue

将跳过打印任何不以tty开头的设备。