将 ATTRidVendor 作为 udev 脚本中的参数传递
Posted
技术标签:
【中文标题】将 ATTRidVendor 作为 udev 脚本中的参数传递【英文标题】:Pass ATTRidVendor as argument in udev script将 ATTRidVendor 作为 udev 脚本中的参数传递 【发布时间】:2012-09-30 21:40:42 【问题描述】:我有一个脚本,只要连接了供应商 1004 的 USB 设备,就会运行该脚本。我使用的 udev 规则有效,看起来像这样。
SUBSYSTEM=="usb", ATTRidVendor=="1004", RUN+="/var/www/beta/trigger.php"
现在我想在连接任何 USB 设备时运行此脚本,并将供应商 ID 作为参数传递。 (因此脚本可以决定是否必须运行。)
到目前为止,添加一个可以在脚本中访问的参数已经奏效了:
SUBSYSTEM=="usb", RUN+="/var/www/beta/trigger.php myparam"
谁能告诉我如何用 ATTRidVendor 的值替换“myparam”?各种组合都试过了,都没有得到预期的结果……
非常感谢!
【问题讨论】:
请编辑您的问题以包括一些“各种组合” 很难说出您的方法以及您尝试使用的工具。祝你好运。 【参考方案1】:为了补充这个答案,udev 还允许您将参数传递给RUN
和PROGRAM
。
来自 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 $attrserial"
可用的替换是:
$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.
$attrfile, %sfile
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.
$envkey, %Ekey
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: %cN. If
the number is followed by the ´+´ char this part plus all remaining
parts of the result string are substituted: %cN+
$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.
【讨论】:
遗憾的是,在较新的版本(我的是 241)中,类似 printf 的语法不受任何模式的支持。手册页类似,仅省略了“printf-like”一词。 :(【参考方案2】:udev
为您设置了几个可以使用的环境变量,其中包括ID_VENDOR
。试试那个小脚本:
#!/bin/bash
echo "Called by udev" >> /tmp/testenv
env >> /tmp/testenv
echo "Vendor id is $ID_VENDOR" >> /tmp/testenv
把它放在一个规则中,你就会看到为你设置了多少东西。
【讨论】:
非常感谢!在 PHP 中,我可以通过 $_SERVER 访问这些环境变量,所以我使用了例如$_SERVER['ID_VENDOR_ID']
供应商 ID。
我测试了这个变量,但文件中没有打印出来!以上是关于将 ATTRidVendor 作为 udev 脚本中的参数传递的主要内容,如果未能解决你的问题,请参考以下文章
我的 perl 脚本如何使用 UDev 而不是 HAL 对任意设备做出反应?
使用 UDEV 和脚本确定光学媒体类型(音频 CD、DVD、蓝光)
为啥 udev init 脚本默认禁用容器支持,而实际上它可以工作?