Bash autorun bash script on USB connect fails to get USB info

82 views Asked by At

I wrote a script to get all the USB info. So far when I run the script with the USB already connected everything works fine.

USB_NAME=$(blkid | grep -oP '(?<=LABEL=").+?(?=")' | tail -1)
USB_DEVICE=$(blkid -o device | tail -1)
USB_TYPE=$(blkid | grep -oP '(?<=TYPE=").+?(?=")' | tail -1)

However, when I try to let the autorun script kick by inserting the USB with the 10.usb-rules:

KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", RUN+="/home/user/test.sh"

It runs the script and everything and i see the output from my script, but the output of my USB_NAME, USB_DEVICE and USB_TYPE all come out blank.

Is it trying to grab the info too quick or something?

1

There are 1 answers

0
tiger_groove On BEST ANSWER

Issue fixed by adding sudo in front of each statement.

USB_NAME=$(sudo blkid | grep -oP '(?<=LABEL=").+?(?=")' | tail -1) 2>&1
USB_DEVICE=$(sudo blkid -o device | tail -1) 2>&1
USB_DEVICE_NAME=$(sudo blkid -o device | cut -d'/' -f3 | tail -1) 2>&1
USB_TYPE=$(sudo blkid | grep -oP '(?<=TYPE=").+?(?=")' | tail -1) 2>&1