Skip to content

Instantly share code, notes, and snippets.

@bkw777
Last active August 7, 2021 00:15
Show Gist options
  • Save bkw777/a20721b09e79eb883683cdae25700c8b to your computer and use it in GitHub Desktop.
Save bkw777/a20721b09e79eb883683cdae25700c8b to your computer and use it in GitHub Desktop.
Platform detection in bash without external executables (no /bin/uname)
#!/usr/bin/env bash
# Example to detect OS in bash using bash built-in variable $OSTYPE instead of `uname`
# platform differences
stty_f="-F" SERIAL_TTY_PREFIX=ttyUSB # Default (Linux)
case "${OSTYPE,,}" in
*bsd*) stty_f="-f" SERIAL_TTY_PREFIX=ttyU ;; # FreeBSD/NetBSD/OpenBSD/etc
darwin*) stty_f="-f" SERIAL_TTY_PREFIX=cu.usbserial- ;; # Mac OSX
esac
x=(/dev/${SERIAL_TTY_PREFIX#/dev/}*)
PS3="Which serial port? "
select PORT in ${x[*]} ;do [[ -c "$PORT" ]] && break ;done
echo "stty ${stty_f} $PORT 19200"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment