Skip to content

Instantly share code, notes, and snippets.

@takuzoo3868
Last active December 11, 2023 07:32
Show Gist options
  • Save takuzoo3868/730000af7491dc354b0d38510493f982 to your computer and use it in GitHub Desktop.
Save takuzoo3868/730000af7491dc354b0d38510493f982 to your computer and use it in GitHub Desktop.
Shell script for determining Linux distribution
#!/bin/sh
# Linux(Distribution) MacOS SunOS AIX に対応
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`
GetVersionFromFile() {
VERSION="$(tr "\n" ' ' < cat "$1" | sed s/.*VERSION.*=\ // )"
}
if [ "${OS}" = "SunOS" ] ; then
OS=Solaris
ARCH=$(uname -p)
OSSTR="${OS} ${REV}(${ARCH} $(uname -v)"
echo ${OSSTR}
return
elif [ "${OS}" = "AIX" ] ; then
OSSTR="${OS} $(oslevel) ($(oslevel -r)"
echo ${OSSTR}
return
elif [ "${OS}" = "Linux" ] ; then
KERNEL=$(uname -r)
if [ -f /etc/redhat-release ] ; then
DIST='RedHat'
PSUEDONAME=$(sed s/.*\(// < /etc/redhat-release | sed s/\)//)
REV=$(sed s/.*release\ // < /etc/redhat-release | sed s/\ .*//)
elif [ -f /etc/SuSE-release ] ; then
DIST=$(tr "\n" ' ' < /etc/SuSE-release | sed s/VERSION.*//)
REV=$(tr "\n" ' ' < /etc/SuSE-release| sed s/.*=\ //)
elif [ -f /etc/mandrake-release ] ; then
DIST='Mandrake'
PSUEDONAME=$(sed s/.*\(// < /etc/mandrake-release | sed s/\)//)
REV=$(sed s/.*release\ // < /etc/mandrake-release | sed s/\ .*//)
elif [ -f /etc/debian_version ] ; then
if [ "$(awk -F= '/DISTRIB_ID/ {print $2}' /etc/lsb-release)" = "Ubuntu" ]; then
DIST="Ubuntu"
else
DIST="Debian $(cat /etc/debian_version)"
REV=""
fi
elif [ -f /etc/arch-release ] ; then
DIST="Arch"
fi
if [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[$(tr "\n" ' ' < /etc/UnitedLinux-release | sed s/VERSION.*//)]"
fi
OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
echo ${OSSTR}
return
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"
}
echo ${OSSTR}
return
fi
# F0ck wind0ws. G0 t0 he11!
echo "Your platform ($(uname -a)) is not supported."
exit 1
@alshdavid
Copy link

alshdavid commented Jul 5, 2023

Trying something like this:

#!/bin/sh

function get_os_details() {
  OS=`uname -s`
  REV=`uname -r`
  MACH=`uname -m`

  if [ "${OS}" == "Darwin" ]; then
    type -p sw_vers &>/dev/null
    if [ $? -eq 0 ]; then
      OS=`sw_vers | grep 'ProductName' | cut -f 3`
      VER=`sw_vers | grep 'ProductVersion' | cut -f 3`
      OSSTR="Darwin ${OS} ${VER}"
    else
      OSSTR="macOS"
    fi
    echo ${OSSTR}
    return
  fi

  if [ "${OS}" = "Linux" ] && [ -f /etc/os-release ]; then
    source /etc/os-release
    echo "Linux ${ID} ${VERSION_ID}"
    return
  fi

  if [ -f /etc/arch-release ]; then
    echo "Linux arch"
    return
  fi

  echo "Your platform ($(uname -a)) is not supported."
  return 1
}

get_os_details

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment