I've been trying to use udev to make a Debian system run a bash script when a wireless card is connected.
So far I created this file /etc/udev/rules.d/wifi-detect.rules:
ACTION=="add", ATTRS{idVendor}=="0cf3", ATTRS{idProduct}=="9271", RUN+="/root/test.sh"
And for now, I'm trying to make test.sh with this contents work:
#!/bin/bash
/bin/echo "test!" > /test.txt
But for some reason, nothing seems to happen when I connect the wireless card, no test.txt file is created.
My lsusb on the card:
Bus 001 Device 015: ID 0cf3:9271 Atheros Communications, Inc. AR9271 802.11n
Running udevadm monitor –env this is what happens when I connect the card:
KERNEL[1017.642278] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3 (usb)
KERNEL[1017.644676] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
KERNEL[1017.645035] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
KERNEL[1017.708056] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
UDEV  [1017.714772] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3 (usb)
UDEV  [1017.733002] remove   /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
UDEV  [1017.772669] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/firmware/1-1.3 (firmware)
UDEV  [1017.798707] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0 (usb)
KERNEL[1018.456804] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/ieee80211/phy8 (ieee80211)
KERNEL[1018.465994] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan0 (net)
KERNEL[1018.479878] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/leds/ath9k_htc-phy8 (leds)
KERNEL[1018.483074] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/usb_device/usbdev1.20 (usb_device)
UDEV  [1018.600456] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/leds/ath9k_htc-phy8 (leds)
UDEV  [1018.604376] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/ieee80211/phy8 (ieee80211)
UDEV  [1018.626243] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/usb_device/usbdev1.20 (usb_device)
KERNEL[1018.659318] move     /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan1 (net)
UDEV  [1018.758843] add      /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan1 (net)
UDEV  [1018.932207] move     /devices/platform/bcm2708_usb/usb1/1-1/1-1.3/1-1.3:1.0/net/wlan1 (net)
I've tried a lot of examples around but I can't make it work. I hope someone can help me out with this one ;) Thank you!
EDIT:
To simplify thing, I changed my rule to:
ACTION=="add", ATTRS{idVendor}=="0cf3", ATTRS{idProduct}=="9271", RUN+="/bin/echo 'test' > /test.txt"
I managed to set udevadm control --log-priority=info as @user1146332 suggested and I got this interesting log: 
Sep  9 16:27:53 iklive-rpi1 udevd[1537]: RUN '/bin/echo 'test' > /test.txt' /etc/udev/rules.d/wifi-detect.rules:1
Sep  9 16:27:53 iklive-rpi1 udevd[1544]: starting 'firmware.agent'
Sep  9 16:27:53 iklive-rpi1 udevd[126]: seq 663 queued, 'remove' 'firmware'
Sep  9 16:27:53 iklive-rpi1 udevd[126]: seq 663 forked new worker [1547]
Sep  9 16:27:53 iklive-rpi1 udevd[1537]: 'firmware.agent' [1544] exit with return code 0
Sep  9 16:27:53 iklive-rpi1 udevd[1548]: starting '/bin/echo 'test' > /test.txt'
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: seq 663 running
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: no db file to read /run/udev/data/+firmware:1-1.3.4: No such file or directory
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: passed -1 bytes to netlink monitor 0x1af5ee0
Sep  9 16:27:53 iklive-rpi1 udevd[126]: seq 663 done with 0
Sep  9 16:27:53 iklive-rpi1 udevd[1547]: seq 663 processed with 0
Sep  9 16:27:53 iklive-rpi1 udevd[1537]: '/bin/echo 'test' > /test.txt'(out) 'test > /test.txt'
Sep  9 16:27:53 iklive-rpi1 udevd[1537]: '/bin/echo 'test' > /test.txt' [1548] exit with return code 0
So... Isn't the return code 0 the exit code for successful completion? If so why I don't get any file on the system?
EDIT 2:
I managed to get this working using the tip by @htor. My current rule:
ACTION=="add", ATTRS{idVendor}=="0cf3", ATTRS{idProduct}=="9271", RUN+="/bin/sh -c '/bin/echo test >> /test.txt'"
But by some reason the command is executed like 8 times, is there a way to avoid this? I think is is happening because when the wireless card drivers are being load they need to virtually unmount and mount the card. Tips?
/bin/echowas executed successfully as your log suggests. The output of your command istest > /test.txtas your log states. The reason for this is, that the character>has no special meaning at all in your context. It's just the third command line argument you passed toecho. You get what you want if you let your shell interpret the given line/bin/echo 'test' > /test.txt. Like you've did in your second EDIT. For instance if you letudevexecutetouch /test.txtin contrast to what you've did you would see a new file in your root.