Skip to content

Instantly share code, notes, and snippets.

@smittytone
Created Sep 8, 2022
Embed
What would you like to do?
Bash shell script for macOS which detects and lists USB-connected serial devices
#!/bin/bash
# Save script as `dlist` at a location on your $PATH.
# Run `chmod +x /path/to/dlist`
#
# Connect a USB serial device, eg. a Raspberry Pi Pico, and then
# enter `dlist` at the command line.
#
# You should see something like:
# `/dev/cu.usbserial-01AB8E0B`
#
# If you have only one device attached, you can include the function's
# output in other command line call. For example:
#
# `pyboard -d $(dlist) -f cp prefs.json :prefs.json`
#
# If you have multiple devices attached, `dlist` will list them all:
#
# `1. /dev/cu.usbmodem1101
# 2. /dev/cu.usbserial-01AB8E0B`
#
# You can then specify a single device by number. For example:
#
# `pyboard -d $(dlist 2) -f cp prefs.json :prefs.json`
#
# NOTE Requires macOS
devices=($(/bin/ls /dev/cu.usb* 2>/dev/null))
if [[ ${#devices[@]} -gt 0 ]]; then
if [[ ${#devices[@]} -eq 1 ]]; then
echo ${devices}
else
count=1
for device in "${devices[@]}"; do
if [[ -n "$1" && "$1" == ${count} ]]; then
echo ${device}
elif [[ -z "$1" ]]; then
echo "${count}. ${device}"
fi
((count+=1))
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment