Skip to content

Instantly share code, notes, and snippets.

@Brianetta
Created September 18, 2019 02:56
Show Gist options
  • Save Brianetta/c70b1f6f67557b1f9c5add9209f73c00 to your computer and use it in GitHub Desktop.
Save Brianetta/c70b1f6f67557b1f9c5add9209f73c00 to your computer and use it in GitHub Desktop.
bash script to give USB device information from a node in /dev
#!/bin/bash
# Brian Ronald, 2019
# Tested in Ubuntu 18.04
# Free for all purposes, no warranty at all.
if [ $# -eq 0 ]
then
echo "Usage: $0 <block device node>"
echo "Show the USB device associated with a block device"
exit
fi
if [ ! -b $1 ]
then
echo "Specified argument is not a block device node"
exit 1
fi
# Ask lsblk for the major and minor nodes, using xargs to strip whitespace
BlockDevice=$(lsblk -ldn $1 -oMAJ:MIN | xargs)
# Look at the device file symlinked in /sys/dev/block to get the device path
DevicePath=$(readlink -f /sys/dev/block/$BlockDevice)
# Look at the path to determine the presence of a USB device
USBPath=$(echo $DevicePath | grep -Po '^/.*usb./.*?/')
if [ -z $USBPath ]
then
echo "Specified device is not connected to USB"
exit 2
fi
# Use the busnum and devnum to get the USB information from lsusb
lsusb -s $(cat $USBPath/busnum):$(cat $USBPath/devnum)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment