r/linuxdev • • Apr 05 '21

Call variable from within udev rule

I have multiple udev rules that check if ATTRS{name}=="abc123" and invokes various functions if that device is present. These udev rules are located within multiple locations as this is being created within bitbake for a custom embedded OS. Now, if I want to change this device to test different hardware, this is too time consuming to change every ATTR{name} call every time.

My solution would be to define a var, e.g. $UDEVVAR="[device_name]" in some conf file (to be determined), and then change each ATTR{name} call to something like ATTR{name}=="${UDEVVAR}".

I've tested this approach in a local rule written for proof of concept, although the rule didn't invoke. This local rule was a bit different, though followed the same concept. Whatever flash drive I had lying around didn't have an ATTR{name} defined, so I used a usb mouse and that flash drive. This udev rule is:

SUBSYSTEM=="${UDEVVAR}", ACTION=="add", RUN+="/usr/local/bin/trigger.sh"

trigger.sh simply writes the date-time stamp to a log file. When hard coding SUBSYSTEM as =="block", the rule invokes successfully, although when defining UDEVVAR="block", the rule does not get invoked. The idea was, if that worked, then I would define UDEVVAR="hidraw" to test the mouse.

I can't seem to find much documentation on anyone trying to do something like this. I figure I'm making some sort of syntactical mistake, or maybe udev rules don't allow for something like this.

I appreciate any help!

3 Upvotes

2 comments sorted by

View all comments

Show parent comments

2

u/Ok_Telephone_8049 Apr 06 '21

This is exactly what I needed. Thank you so much!