Last active
August 8, 2024 20:17
-
-
Save JoeyBurzynski/cb738cbc6a9f866d9f84c2f94e126778 to your computer and use it in GitHub Desktop.
Determine Active Operating System in Bash Script
This file contains hidden or 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
#!/usr/bin/env bash | |
# * Determine operating system via $OSTYPE | |
function active_operating_system_ostype() { | |
echo | |
echo "Determining active operating system via \$OSTYPE" | |
echo " » \$OSTYPE output: $OSTYPE" | |
local active_os | |
if [[ "$OSTYPE" == "linux-gnu"* ]]; then | |
active_os="Linux" | |
elif [[ "$OSTYPE" == "darwin"* ]]; then | |
active_os="MacOS" | |
elif [[ "$OSTYPE" == "cygwin" ]]; then | |
# * POSIX compatibility layer and Linux environment emulation for Windows | |
active_os="Windows" | |
elif [[ "$OSTYPE" == "msys" ]]; then | |
# * Lightweight shell and GNU utilities compiled for Windows (part of MinGW) | |
active_os="Windows" | |
elif [[ "$OSTYPE" == "win32" ]]; then | |
active_os="Windows" | |
elif [[ "$OSTYPE" == "freebsd"* ]]; then | |
active_os="FreeBSD" | |
else | |
active_os="Unknown" | |
fi | |
echo " ✔ Active Operating System: ${active_os}" | |
} | |
active_operating_system_ostype | |
# * Determine operating system using uname | |
function active_operating_system_uname() { | |
local uname_output | |
local active_os | |
uname_output="$(uname -s)" | |
echo | |
echo "Determining active operating system via uname -s" | |
echo " » uname output: $uname_output" | |
case "${uname_output}" in | |
Linux*) active_os=Linux;; | |
Darwin*) active_os=MacOS;; | |
CYGWIN*) active_os=Cygwin;; | |
MINGW*) active_os=MinGw;; | |
FreeBSD*) active_os=FreeBSD;; | |
*) active_os="UNKNOWN:${uname_output}" | |
esac | |
echo " ✔ Active Operating System: ${active_os}" | |
# * Linux | |
if [[ "$active_os" == "Linux" ]]; then | |
echo " » Executing Linux logic.." | |
# * MacOS | |
elif [[ "$active_os" == "MacOS" ]]; then | |
echo " » Executing Mac logic.." | |
else | |
echo " » Unknown operating system. Aborting." | |
exit 1 | |
fi | |
} | |
active_operating_system_uname |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment