Skip to content

Instantly share code, notes, and snippets.

@pyther
Created October 13, 2011 12:44
Show Gist options
  • Save pyther/1284145 to your computer and use it in GitHub Desktop.
Save pyther/1284145 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Detects Linux Distribution
#
# Many Distributions have lsb_release or use /etc/lsb_release
# For those that do not we have some fallback cases
#
# The goal is to report the Distribution and Version that is being used
# An icon will be display based on the first word of the distro's name.
# Example: Scientific Linux 6.1 will be Scientific.png or Scientific.gif
OS=$(uname -s)
VER=$(uname -r)
# Look for lsb_release in path
LSB_REL=$(which lsb_release 2>/dev/null)
if [[ $OS == Linux ]]; then
# If lsb_release is executable set the DIST and VER
if [[ -f /etc/redhat-release ]]; then
DIST=$(sed 's/ release.*//' /etc/redhat-release)
VER=$(sed 's/.*release\ //' /etc/redhat-release | sed 's/\ .*//')
CODENAME=$(sed 's/.*(//' /etc/redhat-release | sed 's/)//')
elif [[ -f /etc/SuSE-release ]]; then
DIST=$(head -1 suse | awk '{print $1}')
VER=$(awk '/VERSION/ {print $3}' /etc/SuSE-release)
CODENAME=$(awk '/CODENAME/ {print $3}' /etc/SuSE-release)
elif [[ -f /etc/arch-release ]]; then
DIST="Arch Linux"
VER=""
# Last time full system upgrade ran
# Can take a while to parse log (depending on log size)
#VER=$(grep 'starting full system upgrade' /var/log/pacman.log | tail -1 | sed 's/\[//' | sed 's/ .*//')
CODENAME=""
elif [[ -f /etc/gentoo-release ]]; then
DIST="Gentoo"
VER=$(sed 's/.*release //' /etc/gentoo-release)
CODENAME=""
elif [[ -x $LSB_REL ]]; then
DIST=$($LSB_REL -is)
VER=$($LSB_REL -rs)
CODENAME=$($LSB_REL -cs)
elif [[ -f /etc/lsb-release ]]; then
DIST=$(grep 'DISTRIB_ID' /etc/lsb-release | cut -d"=" -f2)
VER=$(grep 'DISTRIB_RELEASE' /etc/lsb-release | cut -d"=" -f2)
CODENAME=$(grep 'DISTRIB_RELEASE' /etc/lsb-release | cut -d"=" -f2)
elif [[ -f /etc/debian_version ]]; then
DIST="Debian"
read VER < /etc/debian_version
else
DIST="${OS}"
fi
# Exceptions
# RHEL uses multiple strings RedHatEnterpriseWS (4.x), RedHatEnterpriseServer (5.x, 6.x)
if [[ $DIST =~ RedHatEnterprise ]] || [[ $DIST =~ "Red Hat Enterprise Linux Server" ]]; then
DIST="RHEL"
fi
OSSTR="${DIST} ${VER}"
else
OSSTR="$OS $VER"
fi
arr=()
while getopts "ncv" opt; do
case $opt in
n)
array+=($DIST)
;;
v)
array+=($VER)
;;
c)
array+=("($CODENAME)")
;;
esac
done
if (( ${#array[*]} )); then
echo "${array[*]}"
else
echo $OSSTR
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment