Skip to content

Instantly share code, notes, and snippets.

@carlessanagustin
Created January 14, 2021 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlessanagustin/7d2c73ca9684c17c997dcd3b85d4584e to your computer and use it in GitHub Desktop.
Save carlessanagustin/7d2c73ca9684c17c997dcd3b85d4584e to your computer and use it in GitHub Desktop.
detect linux OS from packagecloud.io
: '
thanks to https://packagecloud.io/
You can find a list of supported OSes and distributions on our website: https://packagecloud.io/docs#os_distro_version
'
detect_os_deb ()
{
if [[ ( -z "${os}" ) && ( -z "${dist}" ) ]]; then
# some systems dont have lsb-release yet have the lsb_release binary and
# vice-versa
if [ -e /etc/lsb-release ]; then
. /etc/lsb-release
if [ "${ID}" = "raspbian" ]; then
os=${ID}
dist=`cut --delimiter='.' -f1 /etc/debian_version`
else
os=${DISTRIB_ID}
dist=${DISTRIB_CODENAME}
if [ -z "$dist" ]; then
dist=${DISTRIB_RELEASE}
fi
fi
elif [ `which lsb_release 2>/dev/null` ]; then
dist=`lsb_release -c | cut -f2`
os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'`
elif [ -e /etc/debian_version ]; then
# some Debians have jessie/sid in their /etc/debian_version
# while others have '6.0.7'
os=`cat /etc/issue | head -1 | awk '{ print tolower($1) }'`
if grep -q '/' /etc/debian_version; then
dist=`cut --delimiter='/' -f1 /etc/debian_version`
else
dist=`cut --delimiter='.' -f1 /etc/debian_version`
fi
else
unknown_os
fi
fi
if [ -z "$dist" ]; then
unknown_os
fi
# remove whitespace from OS and dist name
os="${os// /}"
dist="${dist// /}"
echo "Detected operating system as $os/$dist."
}
detect_os_rpm ()
{
if [[ ( -z "${os}" ) && ( -z "${dist}" ) ]]; then
if [ -e /etc/os-release ]; then
. /etc/os-release
os=${ID}
if [ "${os}" = "poky" ]; then
dist=`echo ${VERSION_ID}`
elif [ "${os}" = "sles" ]; then
dist=`echo ${VERSION_ID}`
elif [ "${os}" = "opensuse" ]; then
dist=`echo ${VERSION_ID}`
elif [ "${os}" = "opensuse-leap" ]; then
os=opensuse
dist=`echo ${VERSION_ID}`
else
dist=`echo ${VERSION_ID} | awk -F '.' '{ print $1 }'`
fi
elif [ `which lsb_release 2>/dev/null` ]; then
# get major version (e.g. '5' or '6')
dist=`lsb_release -r | cut -f2 | awk -F '.' '{ print $1 }'`
# get os (e.g. 'centos', 'redhatenterpriseserver', etc)
os=`lsb_release -i | cut -f2 | awk '{ print tolower($1) }'`
elif [ -e /etc/oracle-release ]; then
dist=`cut -f5 --delimiter=' ' /etc/oracle-release | awk -F '.' '{ print $1 }'`
os='ol'
elif [ -e /etc/fedora-release ]; then
dist=`cut -f3 --delimiter=' ' /etc/fedora-release`
os='fedora'
elif [ -e /etc/redhat-release ]; then
os_hint=`cat /etc/redhat-release | awk '{ print tolower($1) }'`
if [ "${os_hint}" = "centos" ]; then
dist=`cat /etc/redhat-release | awk '{ print $3 }' | awk -F '.' '{ print $1 }'`
os='centos'
elif [ "${os_hint}" = "scientific" ]; then
dist=`cat /etc/redhat-release | awk '{ print $4 }' | awk -F '.' '{ print $1 }'`
os='scientific'
else
dist=`cat /etc/redhat-release | awk '{ print tolower($7) }' | cut -f1 --delimiter='.'`
os='redhatenterpriseserver'
fi
else
aws=`grep -q Amazon /etc/issue`
if [ "$?" = "0" ]; then
dist='6'
os='aws'
else
unknown_os
fi
fi
fi
if [[ ( -z "${os}" ) || ( -z "${dist}" ) ]]; then
unknown_os
fi
# remove whitespace from OS and dist name
os="${os// /}"
dist="${dist// /}"
echo "Detected operating system as ${os}/${dist}."
if [ "${dist}" = "8" ]; then
_skip_pygpgme=1
else
_skip_pygpgme=0
fi
}
unknown_os ()
{
echo "Unfortunately, your operating system distribution and version are not supported by this script."
echo
echo "You can override the OS detection by setting os= and dist= prior to running this script."
echo
echo "For example, to force CentOS 6: os=el dist=6 ./detect_os.sh"
echo "For example, to force Ubuntu Trusty: os=ubuntu dist=trusty ./detect_os.sh"
echo
exit 1
}
detect_os_deb
detect_os_rpm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment