Created
May 2, 2022 04:30
-
-
Save yakuter/4481c24383872866664618161462283f to your computer and use it in GitHub Desktop.
distro sh function
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
# distro prints the detected operating system including linux distros. | |
# Also parses ID_LIKE for common distro bases. | |
# | |
# Example outputs: | |
# - macos -> macos | |
# - freebsd -> freebsd | |
# - ubuntu, raspbian, debian ... -> debian | |
# - amzn, centos, rhel, fedora, ... -> fedora | |
# - opensuse-{leap,tumbleweed} -> opensuse | |
# - alpine -> alpine | |
# - arch -> arch | |
# | |
# Inspired by https://github.com/docker/docker-install/blob/26ff363bcf3b3f5a00498ac43694bf1c7d9ce16c/install.sh#L111-L120. | |
distro() { | |
if [ "$OS" = "macos" ] || [ "$OS" = "freebsd" ]; then | |
echo "$OS" | |
return | |
fi | |
if [ -f /etc/os-release ]; then | |
( | |
. /etc/os-release | |
if [ "${ID_LIKE-}" ]; then | |
for id_like in $ID_LIKE; do | |
case "$id_like" in debian | fedora | opensuse) | |
echo "$id_like" | |
return | |
;; | |
esac | |
done | |
fi | |
echo "$ID" | |
) | |
return | |
fi | |
} | |
# os_name prints a pretty human readable name for the OS/Distro. | |
distro_name() { | |
if [ "$(uname)" = "Darwin" ]; then | |
echo "macOS v$(sw_vers -productVersion)" | |
return | |
fi | |
if [ -f /etc/os-release ]; then | |
( | |
. /etc/os-release | |
echo "$PRETTY_NAME" | |
) | |
return | |
fi | |
# Prints something like: Linux 4.19.0-9-amd64 | |
uname -sr | |
} | |
arch() { | |
case "$(uname -m)" in | |
aarch64) | |
echo arm64 | |
;; | |
x86_64) | |
echo amd64 | |
;; | |
amd64) # FreeBSD. | |
echo amd64 | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment