Skip to content

Commit

Permalink
Update Syntax for Variables
Browse files Browse the repository at this point in the history
  • Loading branch information
LanikSJ committed Oct 18, 2023
1 parent 254c058 commit 6fb5b5e
Showing 1 changed file with 23 additions and 23 deletions.
46 changes: 23 additions & 23 deletions lsusb
Original file line number Diff line number Diff line change
Expand Up @@ -29,40 +29,40 @@ unknown_dev_num=127
parse () {
# Get the name of the device, it is the first line that ends with a ':'
# Trim the string at the end
name=`echo "$device" | head -2 | grep ":$" | sed -e 's/^ *//g' -e 's/ *:$//g'`
name=$(echo "$device" | head -2 | grep ":$" | sed -e 's/^ *//g' -e 's/ *:$//g')

# Get the speed
speed=`echo "$device" | grep "Speed: " | awk -F':' '{print $2}' | sed 's/^ *[^0-9]*//g; s/ *$//; s/b\/sec$//; s/ //g'`
speed=$(echo "$device" | grep "Speed: " | awk -F':' '{print $2}' | sed 's/^ *[^0-9]*//g; s/ *$//; s/b\/sec$//; s/ //g')

# Get the PID, trim at the end
PID=`echo "$device" | grep "Product ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
PID=$(echo "$device" | grep "Product ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g')

# Get the VID string, trim at the end
VID_all=`echo "$device" | grep "Vendor ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
VID_all=$(echo "$device" | grep "Vendor ID" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g')
# Get the VID
VID=`echo "$VID_all" | awk -F ' ' '{print $1}' | sed 's/^ *//; s/ *$//'`
VID=$(echo "$VID_all" | awk -F ' ' '{print $1}' | sed 's/^ *//; s/ *$//')
# Get the manufacturer string
manufacturer=`echo "$VID_all" | cut -d ' ' -f 2- | sed 's/^ *//; s/ *$//'`
manufacturer=$(echo "$VID_all" | cut -d ' ' -f 2- | sed 's/^ *//; s/ *$//')

# Get the Location Id
location=`echo "$device" | grep "Location ID" | sed -e 's/Location ID://; s/^ *//g; s/ *$//g;'`
location=$(echo "$device" | grep "Location ID" | sed -e 's/Location ID://; s/^ *//g; s/ *$//g;')

# Get the bus number. It's the first two hex digits of the Location ID.
# We'll convert to decimal later
bus_num=`echo "$location" | sed -e 's/^..\(..\).*/\1/;'`
bus_num=$(echo "$location" | sed -e 's/^..\(..\).*/\1/;')
if [[ -z $bus_num ]]; then
bus_num="00"
fi

# Get the device number. It's after the '/' in the Location ID, already decimal.
device_num=`echo "$location" | awk -F'/' '{print $2}'`
device_num=$(echo "$location" | awk -F'/' '{print $2}')
if [[ -z $device_num ]]; then
device_num=$((unknown_dev_num--))
fi
device_num=`printf "%03d" "$device_num"`
device_num=$(printf "%03d" "$device_num")

# Strip off the excess from LocationID for sorting tree.
locationID=`echo "$location" | awk -F'/' '{print $1}'`
locationID=$(echo "$location" | awk -F'/' '{print $1}')

# Special case for "root hub" - follow the (faked) Linux standard.
if [ -z "$PID" ]; then
Expand All @@ -71,7 +71,7 @@ parse () {
fi

# Get the bus number, in hexadecimal. We'll convert to decimal later.
bus_num=`echo "$device" | grep "Bus Number" | sed -e 's/Bus Number://' -e 's/0x//; s/^ *//g' -e 's/ *$//g'`
bus_num=$(echo "$device" | grep "Bus Number" | sed -e 's/Bus Number://' -e 's/0x//; s/^ *//g' -e 's/ *$//g')
if [[ -z $bus_num ]]; then
bus_num="00"
fi
Expand All @@ -87,7 +87,7 @@ parse () {
manufacturer="(Apple Inc.)"

# The PID depends on the speed of the hub, which we deduce from the controller driver.
PID=`echo "$device" | grep "Controller Driver" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g; s/.*\(....\)$/\1/'`
PID=$(echo "$device" | grep "Controller Driver" | awk -F':' '{print $2}' | sed -e 's/0x//; s/^ *//g' -e 's/ *$//g; s/.*\(....\)$/\1/')
case "$PID" in
OHCI) PID="8005"; name="OHCI Root Hub Simulation"; speed="12M" ;;
EHCI) PID="8006"; name="EHCI Root Hub Simulation"; speed="480M" ;;
Expand All @@ -100,12 +100,12 @@ parse () {

# Strip the parentheses from manufacturer name unless so specified.
if [ -z "$parens" ]; then
manufacturer=`echo "$manufacturer" | sed 's/(//; s/)//'`
manufacturer=$(echo "$manufacturer" | sed 's/(//; s/)//')
fi

# Include serial number only if available.
serial_str=""
serial_number=`echo "$device" | grep "Serial Number" | sed 's/Serial Number: //; s/^ *//g; s/ *$//g'`
serial_number=$(echo "$device" | grep "Serial Number" | sed 's/Serial Number: //; s/^ *//g; s/ *$//g')
if [ -n "$serial_number" ]; then
serial_str=" Serial: ""$serial_number"
fi
Expand All @@ -120,13 +120,13 @@ parse () {
if [ -n "$vid_pid" ]; then
# Convert input vid to lower case.
if [ -n "$(echo "$vid_pid" | grep ':')" ]; then
arg_vid=`echo "$vid_pid" | awk -F':' '{print $1}' | sed 's/^0x//' | tr '[A-Z]' '[a-z]'`
arg_vid=$(echo "$vid_pid" | awk -F':' '{print $1}' | sed 's/^0x//' | tr '[A-Z]' '[a-z]')
if [ -n "$arg_vid" ]; then
if [ $((16#$arg_vid)) -ne $((16#$VID)) ]; then
return 1
fi
fi
arg_pid=`echo "$vid_pid" | awk -F':' '{print $2}' | sed 's/^0x//' | tr '[A-Z]' '[a-z]'`
arg_pid=$(echo "$vid_pid" | awk -F':' '{print $2}' | sed 's/^0x//' | tr '[A-Z]' '[a-z]')
if [ -n "$arg_pid" ]; then
if [ $((16#$arg_pid)) -ne $((16#$PID)) ]; then
return 1
Expand All @@ -143,14 +143,14 @@ parse () {
if [ -n "$bus_dev" ]; then
if [ -n "$(echo "$bus_dev" | grep ':')" ]; then
# Convert input bus to lower case
arg_bus=`echo "$bus_dev" | awk -F':' '{print $1}' | sed 's/^ *//; s/ *$//'`
arg_bus=$(echo "$bus_dev" | awk -F':' '{print $1}' | sed 's/^ *//; s/ *$//')
if [ -n "$arg_bus" ]; then
if [ "$arg_bus" -ne "$bus_num" ]; then
return 1
fi
fi
# Strip leading and trailing spaces from argument.
arg_dev=`echo "$bus_dev" | awk -F':' '{print $2}' | sed 's/^ *//; s/ *$//'`
arg_dev=$(echo "$bus_dev" | awk -F':' '{print $2}' | sed 's/^ *//; s/ *$//')
if [ -n "$arg_dev" ]; then
if [ "$arg_dev" -ne "$device_num" ]; then
return 1
Expand All @@ -161,7 +161,7 @@ parse () {
# Special case: no colon after -s means device-only.
if [ -z "$(echo "$bus_dev" | grep ':')" ]; then
# Strip leading and trailing spaces from argument.
arg_dev=`echo "$bus_dev" | sed 's/^ *//; s/ *$//'`
arg_dev=$(echo "$bus_dev" | sed 's/^ *//; s/ *$//')
if [ -n "$arg_dev" ]; then
if [ "$arg_dev" -ne "$device_num" ]; then
return 1
Expand Down Expand Up @@ -206,7 +206,7 @@ buildtreeline () {
# So we start each line with the LocationID for sorting purposes, then append the rest of the line as we want it
# Later, we'll do the sort, then strip off the LocationID for display purposes

spaces=`echo "$locationID" | sed 's/^0x...//; s/0//g; s/./ /g; s/.*/&&&&/'`
spaces=$(echo "$locationID" | sed 's/^0x...//; s/0//g; s/./ /g; s/.*/&&&&/')
if [ ${#spaces} -eq 0 ]; then
spaces=" /: "
else
Expand Down Expand Up @@ -246,10 +246,10 @@ tree () {
done

# Strip off final \n.
treedata=`echo "${treedata}" | awk '{ prev_line = line; line = $0; } NR > 1 { print prev_line; } END { ORS = ""; print line; }'`
treedata=$(echo "${treedata}" | awk '{ prev_line = line; line = $0; } NR > 1 { print prev_line; } END { ORS = ""; print line; }')

# Sort by Location ID.
treedata=`echo "${treedata}" | sort`
treedata=$(echo "${treedata}" | sort)

# Now strip off leading Location ID and print to stdout.
for line in $treedata
Expand Down

0 comments on commit 6fb5b5e

Please sign in to comment.