2012-10-10 98 views
7

我有一個腳本,只要連接了供應商1004的USB設備,就會運行該腳本。我正在使用的udev規則工作,看起來像這樣。現在將ATTR {idVendor}作爲udev腳本中的參數傳遞

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

我想有每當任何USB設備連接該腳本運行,並通過供應商ID作爲參數。 (所以該腳本可以決定是否有要運行與否。)

添加可在腳本訪問迄今的工作參數:

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

有人能告訴我如何更換「myparam」的值爲ATTR {idVendor}?我嘗試了各種組合,但我從來沒有得到預期的結果...

非常感謝!

+0

請編輯你的問題包括一些「各種組合」難以分辨你是怎麼回事接近,以及您嘗試使用的工具。祝你好運。 – shellter

回答

7

udev爲您設置了幾個可以使用的環境變量,其中包括ID_VENDOR。試試這個小腳本:

#!/bin/bash 

echo "Called by udev" >> /tmp/testenv 
env >> /tmp/testenv 
echo "Vendor id is $ID_VENDOR" >> /tmp/testenv 

把它放在一個規則中,你會看到爲你設置了多少東西。

+1

非常感謝!在PHP中,我可以通過$ _SERVER訪問這些環境變量,所以我使用了例如'$ _SERVER ['ID_VENDOR_ID']'供應商ID。 – joshtucker

+0

我測試這個變量,但沒有文件打印在文件中! –

17

只是添加到這個答案,udev也可以讓你傳遞參數RUNPROGRAM

從udev的手冊頁:

The NAME, SYMLINK, PROGRAM, OWNER, GROUP, MODE and RUN fields support simple 
    printf-like string 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. 

例如,你可以有這樣的規則:

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

可用替換:

$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 attribute found at the device, where all keys of 
     the rule have matched. If the matching device does not have such an 
     attribute, follow the chain of parent devices and use the value of the 
     first attribute that matches. If the attribute 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 string returned by the external program requested with PROGRAM. A 
     single part of the string, separated by a space character may be 
     selected by specifying the part number as an attribute: %c{N}. If 
     the number is followed by the ´+´ char this part plus all remaining 
     parts of the result string 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.