Passer ATTR {idVendor} comme argument dans le script udev

J’ai un script qui est exécuté chaque fois qu’un périphérique USB par le fournisseur 1004 est connecté. La règle udev que j’utilise fonctionne et ressemble à ceci.

SUBSYSTEM=="usb", ATTR{idVendor}=="1004", RUN+="/var/www/beta/sortinggger.php" 

Maintenant, j’aimerais que ce script soit exécuté chaque fois que n’importe quel périphérique USB est connecté et transmettez l’ID du fournisseur en tant que paramètre. (Le script peut donc décider s’il doit être exécuté ou non.)

L’ajout d’un paramètre accessible dans le script a fonctionné jusqu’à présent:

 SUBSYSTEM=="usb", RUN+="/var/www/beta/sortinggger.php myparam" 

Quelqu’un peut-il s’il vous plaît me dire comment remplacer “myparam” avec la valeur de ATTR {idVendor}? J’ai essayé toutes sortes de combinaisons, mais je n’ai jamais eu le résultat escompté …

Merci beaucoup!

udev définit pour vous plusieurs variables environnementales que vous pouvez utiliser, entre autres ID_VENDOR . Essayez ce petit script:

 #!/bin/bash echo "Called by udev" >> /tmp/testenv env >> /tmp/testenv echo "Vendor id is $ID_VENDOR" >> /tmp/testenv 

Mettez-le dans une règle et vous verrez combien de choses sont mises en place pour vous.

Juste pour append à cette réponse, udev vous permet également de transmettre des arguments à RUN et à PROGRAM .

De la page de manuel udev:

  The NAME, SYMLINK, PROGRAM, OWNER, GROUP, MODE and RUN fields support simple printf-like ssortingng substitutions. The RUN format chars gets applied after all rules have been processed, right before the program is executed. It allows the use of device properties set by earlier matching rules. For all other fields, substitutions are applied while the individual rule is being processed. 

Par exemple, vous pourriez avoir une règle comme celle-ci:

 # Passes major, minor and serial number as parameters to script. ACTION=="add", SUBSYSTEM=="usb", RUN+="/tmp/test.sh %M %m $attr{serial}" 

Les substitutions disponibles sont:

  $kernel, %k The kernel name for this device. $number, %n The kernel number for this device. For example, ´sda3´ has kernel number of ´3´ $devpath, %p The devpath of the device. $id, %b The name of the device matched while searching the devpath upwards for SUBSYSTEMS, KERNELS, DRIVERS and ATTRS. $driver The driver name of the device matched while searching the devpath upwards for SUBSYSTEMS, KERNELS, DRIVERS and ATTRS. $attr{file}, %s{file} The value of a sysfs atsortingbute found at the device, where all keys of the rule have matched. If the matching device does not have such an atsortingbute, follow the chain of parent devices and use the value of the first atsortingbute that matches. If the atsortingbute is a symlink, the last element of the symlink target is returned as the value. $env{key}, %E{key} A device property value. $major, %M The kernel major number for the device. $minor, %m The kernel minor number for the device. $result, %c The ssortingng returned by the external program requested with PROGRAM. A single part of the ssortingng, separated by a space character may be selected by specifying the part number as an atsortingbute: %c{N}. If the number is followed by the ´+´ char this part plus all remaining parts of the result ssortingng are substituted: %c{N+} $parent, %P The node name of the parent device. $name The current name of the device node. If not changed by a rule, it is the name of the kernel device. $links The current list of symlinks, separated by a space character. The value is only set if an earlier rule assigned a value, or during a remove events. $root, %r The udev_root value. $sys, %S The sysfs mount point. $tempnode, %N The name of a created temporary device node to provide access to the device from a external program before the real node is created. %% The ´%´ character itself. $$ The ´$´ character itself.