3

I have a few different devices which all accomplish the same basic task. I would like to be able to swap them, and regardless of which one is plugged in, have the same symlink. For example, if I have device1, device2, and device3 and I plug one in to my computer, I want a symlink to be created that is called my_device. However I would like programs running on my computer to know which one is plugged in, preferably through the use of an environment variable, i.e. MY_DEVICE_ID="1".

This is what I have so far:

ACTION=="add", SUBSYSTEM=="usb" ATTRS{...}=="...", SYMLINK+="my_device", ENV{MY_DEVICE_ID}="1"

ACTION=="add", SUBSYSTEM=="usb" ATTRS{....}=="....", SYMLINK+="my_device", ENV{MY_DEVICE_ID}="2"

ACTION=="add", SUBSYSTEM=="usb" ATTRS{.....}==".....", SYMLINK+="my_device", ENV{MY_DEVICE_ID}="3"

Note: Only one device will ever be plugged in at a time. These rules correctly create the symlink, however an environment variable is never set. Why not, and what can I do to accomplish this?

Thanks for your help!

Ubuntu 14.04

RoboCop87
  • 825
  • 1
  • 8
  • 21

1 Answers1

2

The scope of the variable assigned in the udev rule is limited to the udev environment itself. You need to use another facility to make your data available to other programs.

Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • I was afraid that was the case. Do you have any thoughts on other facilities I can use to accomplish this? – RoboCop87 Jul 09 '15 at 20:32
  • for example you could create another symlink with the device id embedded and then check that – Alex P. Jul 09 '15 at 20:35
  • Yes I suppose you could do that. Unfortunately that would lead to a lot of if statements trying to see which symlink exists, which isn't desirable. Thank you for your suggestion though! – RoboCop87 Jul 09 '15 at 20:39