I'm trying to find a way to start a bash script when any USB device is connected to my PC (USB sticks, mouse, keyboard, etc). This script then takes care of asking the user if he wants to authorize the connected device or not through a pop-up made with zenity. My problem is the trigger that starts the script, searching on the internet I understood that the simplest way is using a udev rule, but from the various online guides I didn't understand how to write it. It is important that when the rule calls the script it passes it the ID (Vendor ID + Product ID) of the device or the path (/sys/bus/usb/DEVICE) so the script can retrieve some information about the device to show it to the user. For the bash script I should know how to do it.
This is a diagram of my idea:
┏╺╺╺╺╺╺╺╺╺╺╺┓
╏ Udev Rule╏
┗╺╺╺╺╺╺╺╺╺╺╺┛
╏
▼
┏╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺┓
╏ Invoke the script ╏
╏ passing as arguments╏
╏ the product ID and ╏
╏ vendor ID or the ╏
╏ path of the device ╏
╏ in the filesystem ╏
┗╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺┛
╏
▼
┏╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺┓
╏ The script asks the ╏
╏ user if he wants to ╏
╏ authorize the ╏
╏ connected device ╏
┗╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺┛
╏ ╏
╏ ╏
▼ ▼
┏╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺┓ ┏╺╺╺╺╺╺╺╺╺╺╺╺╺╺┓
╏ If the user ╏ ╏ If the user ╏
╏ chooses no, ╏ ╏ chooses yes,╏
╏ permission is╏ ╏ permission ╏
╏ not granted ╏ ╏ is granted ╏
┗╺╺╺╺╺╺╺╺╺╺╺╺╺╺╺┛ ┗╺╺╺╺╺╺╺╺╺╺╺╺╺╺┛
How I can do that?
I found that the script (display a popup with zenity) was executed, but the popup was not displayed (probably because the script was run as root). I also changed the name of the rule from 99-xxx.rules to 59-xxx.rules
In the end I noticed that there were too many problems getting the script to run from the udev rule (with zenity and the execution time), so I settled on a very simple script that creates a file containing the name of the connected device and another script in user space periodically checks if this file exists and do all the other things.