Skip to content

Instantly share code, notes, and snippets.

@ahogen
Created July 25, 2018 05:54
Show Gist options
  • Save ahogen/cbfee9fb00059f7e00764c3fc973ce61 to your computer and use it in GitHub Desktop.
Save ahogen/cbfee9fb00059f7e00764c3fc973ce61 to your computer and use it in GitHub Desktop.
Automatically scan and print dev list when USB device added or removed.
#!/bin/bash
###############################################################################
# Author: Alex Hogen <[email protected]>
# https://github.com/ahogen
# Date Created: 2018-07-19
#
# Copyright (c) 2018, Alexander Hogen.
#
# Usage:
#
# This is a simple, stupid script I whipped up because I really wanted a
# simple USB device monitor which informed me when a USB device was added
# or removed.
#
# Built with Linux Mint 18.3 (Ubuntu 16.04) and lsusb (usbutils) 007.
#
# MIT License:
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE
###############################################################################
# Get current list (as a string)
devListLast=$(lsusb)
printf "${devListLast}\n"
# Check every second for device changes. If there is a change, print the new
# device list. It's up to the user to figure out which one was added/removed.
while true; do
devListNow=$(lsusb)
if [ "$devListLast" != "$devListNow" ]; then
printf "===============================================\n"
# Would be nice to show a diff instead. I haven't had to do that
# before and wasn't sure how, so I decided not to.
#diff "${devListLast}" "${devListNow}"
printf "${devListNow}\n"
devListLast=${devListNow}
fi
sleep 1
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment