#!/bin/bash # # get-free-dev.sh: returns the first free device name (with number) of a basename # # Example (silly): # ~$ get-free-dev.sh input/js # input/js2 # js0 and js1 are already used by 2 joysticks. A dumb example because the basic # functionality is already like this. # # Usefull example with multiple playstation joystick adapters: # ~$ get-free-dev.sh input/js-ps # input/js-ps0 # # The first js-ps (joystick on playstation adapter) has been plugged. When another # of those adapter gets plugged the result is input/js-ps1. When js-ps0 gets removed # and another gets inserted the now free js-ps0 will be used again. # This lets you have your own "class" of devices similar to "js" "mouse" "event" etc. # # Why not %n in udev: When you have also other joysticks, the number is not continuous # for the same basename: /dev/input/js-foobar0 /dev/input/js-zonk1 /dev/input/js-ps2 # Why not %e: Doesn't reuse old numbers and increases pointless on replugging. # # udev rule example for a playstation 2 joystick usb adapter: # BUS="usb", KERNEL="js*", SYSFS{product}="SmartJoy PLUS Adapter", PROGRAM="/etc/udev/scripts/get-free-dev.sh input/js-ps", SYMLINK="%c" DEV='/dev' if [ -n "${1}" ]; then DEVBASE="${1}" else echo "Usage: `basename ${0}` device-basename" exit 1 fi i=0 while [ -z "${FREEDEV}" ]; do if ! egrep -q "^(N|S):${DEVBASE}${i}$" ${DEV}/.udevdb/*; then FREEDEV="${DEVBASE}${i}" else i=$(($i+1)) fi done echo "${FREEDEV}"