[Hampshire] Fwd: udev device browser script

Top Page

Reply to this message
Author: Peter Salisbury
Date:  
To: Hampshire LUG Discussion List
Subject: [Hampshire] Fwd: udev device browser script
Hi, I developed this little script for looking through the udev device
database and thought it might be of interest to others.
There's probably a much easier way of doing it, but it was fun anyway!

Excuse the HTML post, I thought it might prevent line wrapping.

ATB, Peter


#!/bin/bash
# Allows browsing through the udev device hierarchy to look at attributes etc
while true; do

# First put up a list of highlights from udev's database (udevadm info
--export-db)
# 1st grep defines what's interesting
# 2nd grep (-v) excludes uninteresting devices
# 1st sed indents the E/S lines
# 2nd sed only prints N/P lines if there are nonNP lines before the next NP
# (i.e. only print names/paths with interesting details)
# it puts each N/P line into the hold space (h)
# nonNP lines check if we've printed the NP line (hold buffer==DONE),
# if not: print it and set it to DONE.
# In any case print the nonNP line.

while true; do # loop till device is selected.
DEV=$(udevadm info --export-db \
| grep "^P\|^N\|ID_VENDOR=\|ID_MODEL=\|ID_INPUT_\|S: disk/by-label\|

NAME=\| WACOM_TYPE=" \
  | grep -v "^N: \(ram\|tty\|usbdev\|vcs\|loop\)" \
  | sed "s/^[^NP]: /                /" \
  | sed -n "s/^[NP]: //;TnonNP;h;b;

:nonNP;x;s/^DONE$/DONE/;tdone;p;s/.*/DONE/; :done;x;p" \
| zenity --list \

      --column "Device" \
      --text "Choose a device to examine" \
      --width 500 --height 500 \
  )
if [ "$?" != "0" ]; then exit 1; fi
if [ "${DEV:0:1}" == " " ]; then
  zenity --error --text "Please select a device, not the details"
else
  break
fi


done

# Ask what sort of udevadm call is required
REQ=$(zenity --list \
        --column "Details" \
        --text "Select detail required for $DEV" \
        <<EOF
udevadm info --query=all
udevadm info --attribute-walk
EOF
)
if [ "$?" != "0" ]; then exit 1; fi


# DEV could be a name or a path
if [ "${DEV:0:1}" == "/" ]; then # it's a path
KEY="--path"
else
KEY="--name"
fi

# Execute the udevadm call and display the results
$REQ $KEY $DEV \
| zenity --text-info \

      --width=700 --height=500
if [ "$?" != "0" ]; then exit 1; fi


done # repeat everything until cancel is pressed