2012-10-10 15 views
7

Tôi có một tập lệnh được chạy bất cứ khi nào thiết bị USB của nhà cung cấp 1004 được kết nối. Quy tắc udev tôi đang sử dụng tác phẩm và trông như thế này.Vượt qua ATTR {idVendor} làm đối số trong tập lệnh udev

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

Bây giờ tôi muốn chạy tập lệnh này bất cứ khi nào BẤT K usb thiết bị USB được kết nối và chuyển ID nhà cung cấp làm thông số. (Vì vậy, các kịch bản có thể quyết định nó phải được chạy hay không.)

Thêm một tham số có thể được truy cập trong kịch bản đã làm việc cho đến nay:

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

Có thể ai đó xin vui lòng cho tôi biết làm thế nào để thay thế "myparam" với giá trị của ATTR {idVendor}? Tôi đã thử tất cả các loại kết hợp, nhưng tôi chưa bao giờ có kết quả mong đợi ...

Cảm ơn rất nhiều!

+0

xin vui lòng chỉnh sửa câu hỏi của bạn để bao gồm một số "tất cả các loại kết hợp" tiếp cận và những công cụ bạn đã thử sử dụng. Chúc may mắn. – shellter

Trả lời

7

udev đặt cho bạn một số biến môi trường mà bạn có thể sử dụng, trong số các biến số khác ID_VENDOR. Hãy thử rằng kịch bản nhỏ:

#!/bin/bash 

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

Đặt nó trong một quy tắc, và bạn sẽ thấy bao nhiêu điều được thiết lập cho bạn.

+1

Cảm ơn bạn rất nhiều! Trong PHP, tôi có thể truy cập các biến môi trường này thông qua $ _SERVER, vì vậy tôi đã sử dụng ví dụ: '$ _SERVER ['ID_VENDOR_ID']' cho id nhà cung cấp. – joshtucker

+0

Tôi kiểm tra biến này nhưng không có gì được in trong tệp! –

17

Chỉ cần thêm vào câu trả lời này, udev cũng cho phép bạn chuyển đối số đến RUNPROGRAM.

Từ trang udev người đàn ông:

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. 

Ví dụ, bạn có thể có một quy tắc như thế này:

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

Các thay sẵn là:

$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. 
Các vấn đề liên quan