0

This question already has an answer here:

I need to collect some system info from machines and decide what to do based on some distribution/version -specific conditionals.

To simplify my example, I concocted a fictitious my_function_display_operating_system function. It is a useless function but it frames the problem:

  1. detect the general OS type (, , etc.)
  2. detect some version information about the OS
  3. detect the distribution friendly name

Point 3. is what I most need help with.

Is there a reliable way to read a string indicating the host distribution name?

I am using /etc/issue in the example below as I know of no other way, or set of ways, to get to this information.

I am happy with a it depends answer, I can build a quite involved case esac switch statement if need be.

I just need to know where to look, and I can't download/install VMs for all the distros I know of to "look around" and/or for the name..


# Displays operating system.

function my_function_display_operating_system() {
  local operating_system
  case $my_global_os_type in
    OSX)
      printf -v operating_system \
                'OS X v%s (build %s)' \
                $(sw_vers -productVersion) \
                $(sw_vers -buildVersion)
      ;;
    Linux)
      local -r linux_kernel_version=$(uname -r)
      printf -v operating_system \
                'GNU/Linux (kernel %s)' \
                "$linux_kernel_version"
      if [[ -f /etc/issue ]]; then
        local -r distribution=$(cat /etc/issue | sed 's/ \\n \\l//g')
        operating_system+="\nDistribution: $distribution"
      fi
      ;;
    *)
      operating_system=Unknown
      ;;
  esac
  printf "Operating system: $operating_system\n"
}

Sample output:


> my_function_display_system_information
Operating system   GNU/Linux (kernel 3.5.0-25-generic)
Distribution       Ubuntu 12.10
System memory      1 GB
Bash               v4.2.37(1)-release
Vim                VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Oct 26 2012 16  45  54)
# ... etc ...

marked as duplicate by Renan, jasonwryan, jordanm, George M, derobert Apr 2 '13 at 22:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

Very similar advice given here:

https://superuser.com/questions/11008/how-do-i-find-out-what-version-of-linux-im-running

The short answer:

 ls /etc/*{release,version}

Various distributions will spit out different formats, unfortunately there's no single uniform system. The answer above lists much more specifically some of the most common files to look for.

Not the answer you're looking for? Browse other questions tagged or ask your own question.