Write a bash script that executes a command when a USB device is plugged in/removed

100 views Asked by At

I'm trying to write a bash script so that Linux resets pulseaudio whenever I plug in or remove my USB DAC. So far it looks like this.

#/bin/bash
while :
do
    sleep 5
    if lsusb | grep YOURID
        then
            continue
    fi
    WHAT YOU WANT TO HAPPEN
done

When lsusb detects the device, it executes the command every 5 seconds I'd like to write two scripts, one that runs a command when plugged in, and one that runs a command when it's unplugged. I want the command to run only once instead of every 5 seconds and to work more than once, but I have no idea how to achieve this as my Bash skills are very limited. Any help?

0

There are 0 answers