从udev规则文件向shell脚本传递参数 [英] passing arguments to shell script from udev rules file
问题描述
在规则文件中,通过传递参数"LABEL"和"DEVNAME"进行挂载来执行脚本
In the rules file a script is executed by passing the arguments "LABEL" and "DEVNAME" for mounting
ACTION=="add", RUN+="/appmount/scripts/usb_mount.sh %E{ID_FS_LABEL} %E{DEVNAME}"
在usb_mount.sh文件中,将参数值打印为
In the usb_mount.sh file printing the arguments value as
echo "LABEL: $1 DEVNAME: $2" # this does not work reliably
某些设备的LABEL字段为空,因此将DEVNAME打印为标签.在bash脚本中,我们可以使用双引号将args传递,即使args为null,它也可以正常工作.
Some of the devices have empty LABEL field and hence the DEVNAME is printed as the label. In the bash script we can pass the args in double quotes and it will work even if the args are null.
将args传递给udev规则的等效条件是什么?
What is the equivalent of the same for passing args to udev rules?
此问题的解决方法可能是切换参数的顺序.有什么可靠的方法吗?
The workaround to this problem might be to switch the order of the arguments. Is there any reliable way?
推荐答案
您应该能够使用单引号,而不是您提到的双引号:
You should be able to use single quotes instead of the double quotes you mentioned:
ACTION=="add", RUN+="/appmount/scripts/usb_mount.sh '%E{ID_FS_LABEL}' '%E{DEVNAME}'"
当心:我没有对此进行测试.也许变量替换将在单引号内失败...
Beware: I didn't test this. Maybe variable substitution will fail within single quotes...
从 man udev
引用密钥"RUN":
Quoting from man udev
about the key "RUN":
程序名称和以下参数用空格分隔.单引号可用于指定带空格的参数.
The program name and following arguments are separated by spaces. Single quotes can be used to specify arguments with spaces.
这篇关于从udev规则文件向shell脚本传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!