Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env bash
# parse-usb-devices.sh
#
# Parse the output of the "usb-devices" bash script and filter
# only for Mass Storage devices (Class 08). Include the active
# device speed (to see whether it is USB2 or USB3.
set -e
bus="N/A"
lev="N/A"
port="N/A"
spd="N/A"
serial="N/A"
vendor="N/A"
idVendor="N/A"
idProduct="N/A"
# command info
if [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ "$1" = "-help" ]; then
echo "RaspiBlitz check speed of connected hard disk(s)"
echo "./parse-usb-devices.sh"
exit 1
fi
# check requirements
if ! [ -f "/usr/bin/usb-devices" ]; then
echo "ERROR: /usr/bin/usb-devices is missing"
exit 1
fi
# process output of usb-devices call
while read -r line; do
firstChar=$(echo "${line}" | cut -d" " -f1)
if [ "${firstChar}" = "T:" ]; then
# new device
bus=$(echo "${line}" | cut -d" " -f3 | cut -d"=" -f2)
lev=$(echo "${line}" | cut -d" " -f4 | cut -d"=" -f2)
port=$(echo "${line}" | cut -d" " -f6 | cut -d"=" -f2)
spd=$(echo "${line}" | cut -d" " -f11 | cut -d"=" -f2)
elif [ "${firstChar}" = "S:" ]; then
# get device infos
if $(echo "${line}" | grep -q "SerialNumber"); then
serial=$(echo "${line}" | cut -d"=" -f2)
fi
elif [ "${firstChar}" = "P:" ]; then
idVendor=$(echo "${line}" | cut -d" " -f3 | cut -d"=" -f2)
idProduct=$(echo "${line}" | cut -d" " -f4 | cut -d"=" -f2)
vendor=$(lsusb | grep "${idVendor}:${idProduct}" | awk '{ s = ""; for (i = 7; i <= NF; i++) s = s $i " "; print s }')
elif [ "${firstChar}" = "I:" ]; then
# print all on last line
ifCls=$(echo "${line}" | cut -d" " -f8 | cut -d"=" -f2)
if [ "${ifCls}" = "08(stor.)" ]; then
echo "Port$((bus))-$((lev))-$((port)) USB@$((spd))Mbit/s: ${vendor} (Serial: ${serial})"
fi
fi
done < <(/usr/bin/usb-devices)
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment