get-distro.sh 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. #!/usr/bin/env bash
  2. ## This script prints Linux distro name and its version number
  3. ## e.g. macos, el8, ubuntu20.04
  4. set -euo pipefail
  5. UNAME="$(uname -s)"
  6. case "$UNAME" in
  7. Darwin)
  8. DIST='macos'
  9. VERSION_ID="$(sw_vers | grep 'ProductVersion' | cut -d':' -f 2 | cut -d'.' -f1 | tr -d ' \t')"
  10. SYSTEM="${DIST}${VERSION_ID}"
  11. ;;
  12. Linux)
  13. # /etc/os-release on amazon linux 2 contains both rhel and centos strings
  14. if grep -q -i 'amzn' /etc/*-release; then
  15. DIST='amzn'
  16. VERSION_ID="$(sed -n '/^VERSION_ID=/p' /etc/os-release | sed -r 's/VERSION_ID=(.*)/\1/g' | sed 's/"//g')"
  17. elif grep -q -i 'rhel' /etc/*-release; then
  18. DIST='el'
  19. VERSION_ID="$(rpm --eval '%{rhel}')"
  20. else
  21. DIST="$(sed -n '/^ID=/p' /etc/os-release | sed -r 's/ID=(.*)/\1/g' | sed 's/"//g')"
  22. VERSION_ID="$(sed -n '/^VERSION_ID=/p' /etc/os-release | sed -r 's/VERSION_ID=(.*)/\1/g' | sed 's/"//g')"
  23. fi
  24. SYSTEM="$(echo "${DIST}${VERSION_ID}" | sed -r 's/([a-zA-Z]*)-.*/\1/g')"
  25. ;;
  26. CYGWIN*|MSYS*|MINGW*)
  27. SYSTEM="windows"
  28. ;;
  29. esac
  30. echo "$SYSTEM"