Watch Serial Device Hotplug

Medium Shell Integration Asked at Red Hat
Solve this problem

Write a script that waits for a USB serial device to appear, runs a command against it, and times out cleanly if the device never shows up.

Constraints

  • Poll for /dev/ttyUSB* or /dev/ttyACM*.
  • Timeout is configurable and defaults to 30 seconds.
  • Exit 2 on timeout, 0 on success.
  • Must not busy-spin; sleep between polls.
  • Pass the discovered device path to the command.
  • Handle more than one match by taking the first.
Starter codeShell
1#!/bin/sh2# TODO: wait for serial device then run command