4

I'm trying to make a script that takes the product and vendor id printed by using lsusb, then checking against this ID to find the USB device's directory in /sys/bus/usb/devices.

I initially thought the Bus and Device number printed by lsusb would point to the appropriate folder. For example, if Bus = 002 and Device = 002, the USB's directory would be /usb/devices/2-2. Unfortunately, this turned out to not be the case.

I can manually find the appropriate folder using this command I found in another thread:

for X in /sys/bus/usb/devices/*; do 
    echo "$X"
    cat "$X/idVendor" 2>/dev/null 
    cat "$X/idProduct" 2>/dev/null
    echo
done

However, I need a script that can automate finding this folder.

troylatroy
  • 1,275
  • 1
  • 11
  • 21
user2718585
  • 43
  • 1
  • 3

2 Answers2

4

If I understood your question, the following script should do the job:

#!/bin/bash

if [ $# -ne 2 ];then
  echo "Usage: `basename $0` idVendor idProduct"
  exit 1
fi


for X in /sys/bus/usb/devices/*; do 
    if [ "$1" == "$(cat "$X/idVendor" 2>/dev/null)" -a "$2" == "$(cat "$X/idProduct" 2>/dev/null)" ]
    then
        echo "$X"
    fi
done
Radu Rădeanu
  • 169,590
0

You can actually do this a little simpler:

rgrep -l $1 /sys/bus/usb/devices/*/idVendor | sed 's/\(.*\)\/idVendor/\1/' | xargs -iXXX rgrep -l $2 XXX/idProduct | sed 's/\(.*\)\/idProduct/\1/'

You may want to keep the sanity check though if you want to save this as a script:

#!/bin/bash

if [ $# -ne 2 ];then
  echo "Usage: `basename $0` idVendor idProduct"
  exit 1
fi

rgrep -l ^${1}$ /sys/bus/usb/devices/*/idVendor | sed 's/\(.*\)\/idVendor/\1/' | xargs -iXXX rgrep -l ^${2}$ XXX/idProduct | sed 's/\(.*\)\/idProduct/\1/'
atleta
  • 111
  • 4