I've written a few shell scripts, but have never seen this behavior and am at a loss. I've got the following simple script which runs in a bash shell:
LOGFILE="/var/log/constructor-events.txt"
SUBSYSTEM="$1"
DEVTYPE="$2"
DEVICE="$3"
VENDOR=$(lsusb -D "$DEVICE" | grep idVendor 2>&1)
REQUEST=$(cat <<EOF
{"data": {
"action": "add",
"port": {
"type": "$SUBSYSTEM",
},
"drive": {
"vendor_id": "$VENDOR",
}
}}
EOF
)
printf "output: $VENDOR" >> $LOGFILE
printf "%s\n" "`date +%x\ %r\ %Z` $REQUEST" >> $LOGFILE
This is executed from a udev rule. The positional parameters (coming from udev) have the values I expect, and I have no problem printing them to the log file. But for some reason, the $VENDOR
variable contains no output from the lsusb
command.
Here is the debugging I've done.
- Added stderr redirection to stdout to capture any error that may be occurring.
- Added the line to send the
$VENDOR
variable directly to the log, which is empty. - Executed the script manually in the shell
and printed the
$VENDOR
variable to the terminal and it's empty. Took the string that is contained in the
$DEVICE
variable and executed it directly in the shell, with this result.[root@host ~]# lsusb -D /dev/bus/usb/016/030 | grep idVendor Cannot open /dev/bus/usb/016/030
Why is the $VENDOR
variable empty when outputting to the log file?
EDIT:
Here is an update after removing the redirection from the command substitution.
- Added a line to print the
$VENDOR
variable to the terminal. - Statically assigned the value instead of using the
$DEVICE
variable.
Printing $VENDOR
to the terminal output the error, but it still did not append to the log file! The value of $DEVICE
IS in the log file. I believe it has something to do with the output redirection inside the command substitution.
ls /dev/bus/usb/*
? You can edit hereCannot open /dev/bus/usb/016/030
mean that016:030
device doesn't exist./proc/bus/usb/016: 001 002 053
But it gets that path dynamically from udev, and when i look in the log file 053 is the path it's printing so I know its getting the correct path of the device.lsusb
is missing. But after thinking more about it I think that could be becauselsusb
is not running properly from the udev rule. Possibly because it is not running in the normal shell environment.