Created
September 8, 2022 13:02
-
-
Save smittytone/326a32fd19df281f5684cb07987bcc5f to your computer and use it in GitHub Desktop.
Bash shell script for macOS which detects and lists USB-connected serial devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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