Guest User

Untitled

a guest
Aug 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #!/bin/bash
  2. # Detects Linux Distribution
  3. #
  4. # Many Distributions have lsb_release or use /etc/lsb_release
  5. # For those that do not we have some fallback cases
  6. #
  7. # The goal is to report the Distribution and Version that is being used
  8. # An icon will be display based on the first word of the distro's name.
  9. # Example: Scientific Linux 6.1 will be Scientific.png or Scientific.gif
  10.  
  11. OS=$(uname -s)
  12. VER=$(uname -r)
  13.  
  14. # Look for lsb_release in path
  15. LSB_REL=$(which lsb_release 2>/dev/null)
  16.  
  17. if [[ $OS == Linux ]]; then
  18. # If lsb_release is executable set the DIST and VER
  19. if [[ -f /etc/redhat-release ]]; then
  20. DIST=$(sed 's/ release.*//' /etc/redhat-release)
  21. VER=$(sed 's/.*release\ //' /etc/redhat-release | sed 's/\ .*//')
  22. CODENAME=$(sed 's/.*(//' /etc/redhat-release | sed 's/)//')
  23. elif [[ -f /etc/SuSE-release ]]; then
  24. DIST=$(head -1 suse | awk '{print $1}')
  25. VER=$(awk '/VERSION/ {print $3}' /etc/SuSE-release)
  26. CODENAME=$(awk '/CODENAME/ {print $3}' /etc/SuSE-release)
  27. elif [[ -f /etc/arch-release ]]; then
  28. DIST="Arch Linux"
  29. VER=""
  30. # Last time full system upgrade ran
  31. # Can take a while to parse log (depending on log size)
  32. #VER=$(grep 'starting full system upgrade' /var/log/pacman.log | tail -1 | sed 's/\[//' | sed 's/ .*//')
  33. CODENAME=""
  34. elif [[ -f /etc/gentoo-release ]]; then
  35. DIST="Gentoo"
  36. VER=$(sed 's/.*release //' /etc/gentoo-release)
  37. CODENAME=""
  38. elif [[ -x $LSB_REL ]]; then
  39. DIST=$($LSB_REL -is)
  40. VER=$($LSB_REL -rs)
  41. CODENAME=$($LSB_REL -cs)
  42. elif [[ -f /etc/lsb-release ]]; then
  43. DIST=$(grep 'DISTRIB_ID' /etc/lsb-release | cut -d"=" -f2)
  44. VER=$(grep 'DISTRIB_RELEASE' /etc/lsb-release | cut -d"=" -f2)
  45. CODENAME=$(grep 'DISTRIB_RELEASE' /etc/lsb-release | cut -d"=" -f2)
  46. elif [[ -f /etc/debian_version ]]; then
  47. DIST="Debian"
  48. read VER < /etc/debian_version
  49. else
  50. DIST="${OS}"
  51. fi
  52.  
  53. # Exceptions
  54. # RHEL uses multiple strings RedHatEnterpriseWS (4.x), RedHatEnterpriseServer (5.x, 6.x)
  55. if [[ $DIST =~ RedHatEnterprise ]] || [[ $DIST =~ "Red Hat Enterprise Linux Server" ]]; then
  56. DIST="RHEL"
  57. fi
  58.  
  59. OSSTR="${DIST} ${VER}"
  60. else
  61. OSSTR="$OS $VER"
  62. fi
  63.  
  64. arr=()
  65. while getopts "ncv" opt; do
  66. case $opt in
  67. n)
  68. array+=($DIST)
  69. ;;
  70. v)
  71. array+=($VER)
  72. ;;
  73. c)
  74. array+=("($CODENAME)")
  75. ;;
  76. esac
  77. done
  78.  
  79. if (( ${#array[*]} )); then
  80. echo "${array[*]}"
  81. else
  82. echo $OSSTR
  83. fi
Add Comment
Please, Sign In to add comment