Running the script when it detects I/O

I have bluetooth mouse xinput setting script to run whenever i have connect the mouse. Currently I'm simply alias the short key as 'bm' for that bash file execution but I want to know if there is automatic way to execute the bash file or alias command whenever it detects the mouse connection.

Thank in advance!

ubuntu 16.10


Solution 1:

What you want is to use polling approach, to continuously read output of xinput --list --name-only and figure out if the name of your mouse is there or not, and if it is - run the script. This would be something like this:

while true; do
    if xinput --list --name-only | grep -q -i 'Mouse Name' ; 
    then 
        echo "yes" # this is where you run script
        break # exit the loop after running the script.   
    fi 
done

In this case we exit loop as soon as the mouse is there. However you probably want this to be continuous so that you can connect and disconnect the mouse. Instead of break, I'd use another while loop there, that does the opposite - waits for mouse name to disappear. Body of that while loop can be just this - true or :.