Skip to content

Instantly share code, notes, and snippets.

@ashrithr
Created June 10, 2013 22:18
Show Gist options
  • Save ashrithr/5752922 to your computer and use it in GitHub Desktop.
Save ashrithr/5752922 to your computer and use it in GitHub Desktop.
Detect OS info
#!/bin/bash
# Detects which OS and
# if it is Linux then it will detect which Linux Distribution,
# if Mac(Darwin) will detect mac os x version, build number
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`
GetVersionFromFile()
{
VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
}
if [ "${OS}" == "SunOS" ] ; then
OS=Solaris
ARCH=`uname -p`
OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" == "AIX" ] ; then
OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" == "Linux" ] ; then
KERNEL=`uname -r`
if [ -f /etc/redhat-release ] ; then
DIST='RedHat'
PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/SUSE-release ] ; then
DIST=`cat /etc/SUSE-release | tr "\n" ' '| sed s/VERSION.*//`
REV=`cat /etc/SUSE-release | tr "\n" ' ' | sed s/.*=\ //`
elif [ -f /etc/mandrake-release ] ; then
DIST='Mandrake'
PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/debian_version ] ; then
DIST="Debian `cat /etc/debian_version`"
REV=""
fi
if [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
fi
OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
elif [ "${OS}" == "Darwin" ]; then
type -p sw_vers &>/dev/null
[ $? -eq 0 ] && {
OS=`sw_vers | grep 'ProductName' | cut -f 2`
VER=`sw_vers | grep 'ProductVersion' | cut -f 2`
BUILD=`sw_vers | grep 'BuildVersion' | cut -f 2`
OSSTR="Darwin ${OS} ${VER} ${BUILD}"
} || {
OSSTR="MacOSX"
}
fi
echo ${OSSTR}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment