0

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 ...

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232

1 Answers1

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.

Stephan
  • 2,911