passing arguments to shell script from udev rules file
Asked Answered
P

2

8

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}"

In the usb_mount.sh file printing the arguments value as

echo "LABEL: $1 DEVNAME: $2" # this does not work reliably

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.

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?

Pretzel answered 4/12, 2012 at 9:3 Comment(0)
L
13

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...

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.

Legnica answered 20/2, 2013 at 14:39 Comment(0)
S
3

Just set and query udev's env, here an example to check if system is running VMWare :

cat /etc/udev/rules.d/99-vmware.rules
KERNEL=="id", SUBSYSTEM=="dmi", ATTR{sys_vendor}=="VMware, Inc.", ENV{VMWARE}='defined'

udevadm info -e  | grep 'VMWARE='
E: VMWARE=defined
Stiver answered 9/10, 2014 at 14:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.