Find out wich Linux distro are you using


/ Published in: Bash
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #! /bin/bash
  2. # return an awkable string consisting of
  3. # unix OS type, or
  4. # Linux dist, or
  5. # a long guess (based on /proc), or
  6. # no clue
  7.  
  8. giveUp () {
  9. echo "Unknown"
  10. exit 0
  11. }
  12.  
  13. # keep this easily awkable, prepending an initial clue
  14. versionGuess () {
  15. if [ -e /proc/version ]; then
  16. echo -n "Unsure "
  17. cat /proc/version
  18. exit 0
  19. fi
  20. return 1
  21. }
  22.  
  23. # if we have ignition, print and exit
  24. gotDist () {
  25. [ -n "$1" ] && echo "$1" && exit 0
  26. }
  27.  
  28. # we are only interested in a single word "dist" here
  29. # various malformations can occur; admin will have to code appropately based on output
  30. linuxRelease () {
  31. if [ -r /etc/lsb-release ]; then
  32. dist=$(grep 'DISTRIB_ID' /etc/lsb-release | sed 's/DISTRIB_ID=//' | head -1)
  33. gotDist "$dist"
  34. fi
  35.  
  36. dist=$(find /etc/ -maxdepth 1 -name '*release' 2> /dev/null | sed 's/\/etc\///' | sed 's/-release//' | head -1)
  37. gotDist "$dist"
  38.  
  39. dist=$(find /etc/ -maxdepth 1 -name '*version' 2> /dev/null | sed 's/\/etc\///' | sed 's/-version//' | head -1)
  40. gotDist "$dist"
  41.  
  42. return 1
  43. }
  44.  
  45. # start with uname and branch the decision from there
  46. dist=$(uname -s 2> /dev/null)
  47. if [ "$dist" = "Linux" ]; then
  48. linuxRelease
  49. versionGuess
  50. giveUp
  51. elif [ -n "$dist" ]; then
  52. echo "$dist"
  53. exit 0
  54. else
  55. versionGuess
  56. giveUp
  57. fi
  58.  
  59. # we shouldn't get here
  60. giveUp
  61. # done

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.