Last active
July 11, 2025 12:01
-
-
Save dkittell/fcc82cef813fdc606a518867a33aa81a to your computer and use it in GitHub Desktop.
Unix Identification is a script that originated with VMWare but I have added some other systems to the 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
#!/bin/bash | |
############################################################################################### | |
# PROGRAM: UnixIdentification.sh | |
# DESCRIPTION: Identify the UNIX operating system that you are on | |
# | |
# INITIAL CREDIT: UnixIdentification is a script that originated with VMWare but I have added | |
# some other systems to the script. | |
# | |
# Command line: bash UnixIdentification.sh | |
############################################################################################### | |
# Variables - Begin | |
DISTRO_ID="" | |
DISTRO_NAME="" | |
DISTRO_RELEASE="" | |
DISTRO_DESC="" | |
# Variables - End | |
# Functions - Begin | |
unknown_os() { | |
echo "Unfortunately, your operating system distribution and version are not supported by this script." | |
} | |
identify_distribution() { | |
#region Main Constant Definition | |
# Debian Based | |
DISTRO_ID_UBUNTU=Ubuntu | |
DISTRO_ID_KYLIN=UbuntuKylin | |
DISTRO_ID_ZORIN=Zorin | |
DISTRO_ID_RASPBIAN=Raspbian | |
# Mac | |
DISTRO_ID_DARWIN="Mac OSX (Darwin)" | |
# Red Hat Based | |
DISTRO_ID_CENTOS=CentOS | |
DISTRO_ID_FEDORA=Fedora | |
DISTRO_ID_ROCKY=Rocky | |
DISTRO_ID_RHEL_CLIENT=RedHatEnterpriseClient | |
DISTRO_ID_RHEL_SERVER=RedHatEnterpriseServer | |
DISTRO_ID_RHEL_WORKSTATION=RedHatEnterpriseWorkstation | |
# Other | |
DISTRO_ID_NEOKYLIN=" NeoKylin Linux Desktop" | |
DISTRO_ID_SUSE="SUSE LINUX" | |
DISTRO_ID_AWS="AWS" | |
#endregion Main Constant Definition | |
#region Determine UNIX OS | |
type lsb_release >/dev/null 2>&1 | |
if [ "$?" = "0" ]; then | |
DISTRO_ID="$(lsb_release -is)" | |
DISTRO_RELEASE="$(lsb_release -rs)" | |
DISTRO_DESC="$(lsb_release -ds)" | |
DISTRO_NAME"$(lsb_release -d)" | |
#region OS Check - Debian | |
elif [ -f /etc/lsb-release ]; then | |
. /etc/lsb-release | |
DISTRO_ID=$DISTRIB_ID | |
DISTRO_DESC=$DISTRIB_DESCRIPTION | |
DISTRO_RELEASE=$DISTRIB_RELEASE | |
elif [ -f /etc/lsb-release ] && [ -z DISTRO_ID]; then | |
DISTRO_ID=$(cat /etc/lsb-release | grep '^DISTRIB_ID' | cut -d '=' -f2) | |
DISTRO_DESC=$(cat /etc/lsb-release | grep '^DISTRIB_DESCRIPTION' | cut -d '=' -f2) | |
# DISTRO_RELEASE=$(cat /etc/lsb-release | grep '^DISTRO_RELEASE' | cut -d '=' -f2) | |
DISTRO_RELEASE=$(cat /etc/lsb-release | grep -E '(^DISTRO_RELEASE|^DISTRIB_CODENAME)' | cut -d '=' -f2) | |
DISTRO_NAME="debian" | |
#endregion OS Check - Debian | |
#region OS Check - CentOS | |
elif [ -f /etc/centos-release ]; then | |
DISTRO_ID=$DISTRO_ID_CENTOS | |
DISTRO_DESC="$(cat /etc/centos-release)" | |
DISTRO_RELEASE="$(echo $DISTRO_DESC | awk '{ for (i=1; i < NF; i++) { if ($i == "release") { print $(i+1); break }}}')" | |
DISTRO_NAME="centos" | |
#endregion OS Check - CentOS | |
#region OS Check - Rocky | |
elif [ -f /etc/rocky-release ]; then | |
DISTRO_ID=$DISTRO_ID_ROCKY | |
DISTRO_DESC="$(cat /etc/rocky-release)" | |
DISTRO_RELEASE="$(echo $DISTRO_DESC | awk '{ for (i=1; i < NF; i++) { if ($i == "release") { print $(i+1); break }}}')" | |
DISTRO_NAME="rocky" | |
#endregion OS Check - Rocky | |
#region OS Check - Fedora | |
elif [ -f /etc/fedora-release ]; then | |
DISTRO_ID=$DISTRO_ID_FEDORA | |
DISTRO_DESC="$(cat /etc/fedora-release)" | |
DISTRO_RELEASE="$(echo $DISTRO_DESC | awk '{ for (i=1; i < NF; i++) { if ($i == "release") { print $(i+1); break }}}')" | |
DISTRO_NAME="fedora" | |
#endregion OS Check - Fedora | |
#elif [ -f /etc/issue ]; then | |
#if [ grep -q Amazon /etc/issue ]; then | |
#aws=`grep -q Amazon /etc/issue` | |
#if [ "$?" = "0" ]; then | |
#DISTRO_ID=$DISTRO_ID_AWS | |
#DISTRO_DESC="6" | |
#fi | |
#fi | |
#region OS Check - Mac OSX | |
elif [ $(uname -s) = "Darwin" ]; then | |
DISTRO_ID=$DISTRO_ID_DARWIN | |
DISTRO_NAME="macOS" | |
# Get operating system name and version - Start | |
OSvers1=$(sw_vers -productVersion | cut -d. -f1) | |
OSvers2=$(sw_vers -productVersion | cut -d. -f2) | |
OSvers3=$(sw_vers -productVersion | cut -d. -f3) | |
OSvers="$OSvers1.$OSvers2" | |
#echo $OSvers | |
case "$OSvers" in | |
"10.8") | |
OSName="Mountain Lion" | |
;; | |
"10.9") | |
OSName="Mavericks" | |
;; | |
"10.10") | |
OSName="Yosemite" | |
;; | |
"10.11") | |
OSName="El Capitan" | |
;; | |
"10.12") | |
OSName="Sierra" | |
;; | |
"10.13") | |
OSName="High Sierra" | |
;; | |
"10.14") | |
OSName="Mojave" | |
;; | |
"10.15") | |
OSName="Catalina" | |
;; | |
"11.1" | "11.2" | "11.3" | "11.4" | "11.5") | |
OSName="Big Sur" | |
;; | |
"12.0" | "12.0.1" | "12.1" | "12.2" | "12.2.1" | "12.3" | "12.3.1" | "12.4" | "12.5" | "12.5.1" | "12.6" | "12.6.1") | |
OSName="Monterey" | |
;; | |
"13.0" | "13.0.1" | "13.1" | "13.2" | "13.2.1" | "13.3" | "13.3.1" | "13.4" | "13.4.1" | "13.5" | "13.5.1" | "13.5.2" | "13.6") | |
OSName="Ventura" | |
;; | |
"14.0" | "14.1" | "14.1.1" | "14.1.2" | "14.2" | "14.2.1" | "14.3" | "14.3.1") | |
OSName="Sonoma" | |
;; | |
default) | |
OSName="Unknown - OSvers" | |
;; | |
esac | |
#echo $OSName | |
# Get operating system name and version - Stop | |
OSVers=$OSvers1 | |
if [ ! -z $OSvers2 ]; then | |
OSVers="$OSVers.$OSvers2" | |
fi | |
if [ ! -z $OSvers3 ]; then | |
OSVers="$OSVers.$OSvers3" | |
fi | |
DISTRO_DESC="Mac OS X - $OSName $OSVers" | |
DISTRO_RELEASE="$OSVers" | |
#endregion OS Check - Mac OSX | |
#region OS Check - Red Hat | |
elif [ -f /etc/redhat-release ]; then | |
DISTRO_DESC="$(cat /etc/redhat-release)" | |
DISTRO_ID= | |
echo $DISTRO_DESC | grep "Client" >/dev/null 2>&1 && DISTRO_ID=$DISTRO_ID_RHEL_CLIENT | |
[ -z "$DISTRO_ID" ] && echo $DISTRO_DESC | grep "Server" >/dev/null 2>&1 && DISTRO_ID=$DISTRO_ID_RHEL_SERVER | |
[ -z "$DISTRO_ID" ] && echo $DISTRO_DESC | grep "Workstation" >/dev/null 2>&1 && DISTRO_ID=$DISTRO_ID_RHEL_WORKSTATION | |
[ -z "$DISTRO_ID" ] && echo $DISTRO_DESC >/dev/null 2>&1 && DISTRO_ID=$DISTRO_ID_RHEL_SERVER | |
DISTRO_RELEASE="$(echo $DISTRO_DESC | awk '{ for (i=1; i < NF; i++) { if ($i == "release") { print $(i+1); break }}}')" | |
DISTRO_NAME="rhel" | |
#endregion OS Check - Red Hat | |
else | |
unknown_os | |
exit | |
fi | |
#region Ubuntu Variants | |
SUB='Zorin OS 15' | |
if [[ "$DISTRO_DESC" =~ .*"$SUB".* ]]; then | |
DISTRO_DESC=$(echo "$DISTRO_DESC (Ubuntu 18.04 LTS)") | |
fi | |
# | |
# Differentiate between Ubuntu and Ubuntu Kylin. | |
# | |
if [ "$DISTRO_ID" = "$DISTRO_ID_UBUNTU" ]; then | |
# | |
# Distinguish between Ubuntu and Ubuntu Kylin where possible. Release | |
# files are common place and the following file exists on | |
# Ubuntu Kylin 14.04 LTS, but not on Ubuntu Kylin 13.04. Your mileage | |
# may vary... | |
if [ -f /etc/ubuntukylin-release ]; then | |
DISTRO_ID=$DISTRO_ID_KYLIN | |
fi | |
fi | |
#endregion Ubuntu Variants | |
#region SUSE | |
if [ "$DISTRO_ID" = "SUSE" ]; then | |
# | |
# Add the distribution ID "SUSE" to the DISTRO_ID_SUSE | |
# | |
DISTRO_ID=$DISTRO_ID_SUSE | |
fi | |
#endregion SUSE | |
#endregion Determine UNIX OS | |
# | |
# Check supported distributions | |
# | |
case "$DISTRO_ID" in | |
${DISTRO_ID_CENTOS} | ${DISTRO_ID_ROCKY} | ${DISTRO_ID_FEDORA} | ${DISTRO_ID_RHEL_CLIENT} | ${DISTRO_ID_RHEL_SERVER} | ${DISTRO_ID_RHEL_WORKSTATION} | ${DISTRO_ID_SUSE} | ${DISTRO_ID_NEOKYLIN}) ;; | |
${DISTRO_ID_KYLIN} | ${DISTRO_ID_UBUNTU} | ${DISTRO_ID_ZORIN} | ${DISTRO_ID_RASPBIAN}) | |
#echo $DISTRO_DESC | grep " LTS$" >/dev/null 2>&1 | |
#if [ "$?" != "0" ]; then | |
#echo "$DISTRO_DESC not an LTS release" | |
#exit | |
#fi | |
;; | |
${DISTRO_ID_DARWIN}) ;; | |
*) | |
echo -e "$DISTRO_ID\n$DISTRO_DESC not supported" | |
exit | |
;; | |
esac | |
case "$1" in | |
-a) | |
echo "$DISTRO_ID" | |
echo "$DISTRO_NAME" | |
echo "$DISTRO_RELEASE" | |
echo "$DISTRO_DESC" | |
;; | |
-d) | |
echo "$DISTRO_DESC" | |
;; | |
-r) | |
echo "$DISTRO_RELEASE" | |
;; | |
-i) | |
echo "$DISTRO_ID" | |
;; | |
-n) | |
echo "$DISTRO_NAME" | |
;; | |
*) | |
echo -e "What details would you like to see?\n" \ | |
" -a is for all details.\n" \ | |
" -d is for full OS name with version.\n" \ | |
" -r is for the release version of the OS\n" \ | |
" -i is for the identifier of the OS\n" \ | |
" -n is for the basic name of the distribution (ubuntu, macOS, rhel, etc.)\n" | |
echo "Type your option (a, d, r, i, or n), followed by [ENTER]:" | |
read uiOption | |
case "$uiOption" in | |
a) | |
echo "$DISTRO_ID" | |
echo "$DISTRO_NAME" | |
echo "$DISTRO_RELEASE" | |
echo "$DISTRO_DESC" | |
;; | |
d) | |
echo "$DISTRO_DESC" | |
;; | |
r) | |
echo "$DISTRO_RELEASE" | |
;; | |
i) | |
echo "$DISTRO_ID" | |
;; | |
*) | |
echo "$DISTRO_NAME" | |
;; | |
esac | |
;; | |
esac | |
} | |
# Functions - End | |
identify_distribution $1 | |
#echo $DISTRO_ID |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment