I need to collect some system info from linux 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:
- detect the general OS type (osx, linux, etc.)
- detect some version information about the OS
- 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 linux 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 grep 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 ...