我可以通过几种方式检查运行Ruby代码的平台的操作系统:
RUBY_PLATFORM:https://stackoverflow.com/a/171011/462015RbConfig::CONFIG['host_os']:https://stackoverflow.com/a/13586108/462015有可能知道Linux发行版正在运行吗?例如,基于Debian或基于Red的发行版。
发布于 2014-09-23 15:55:03
正如上面的注释部分所指出的,似乎没有确定的“在每个发行版中工作”的方法来这样做。下面是我用来检测脚本正在运行的环境类型的内容:
def linux_variant
r = { :distro => nil, :family => nil }
if File.exists?('/etc/lsb-release')
File.open('/etc/lsb-release', 'r').read.each_line do |line|
r = { :distro => $1 } if line =~ /^DISTRIB_ID=(.*)/
end
end
if File.exists?('/etc/debian_version')
r[:distro] = 'Debian' if r[:distro].nil?
r[:family] = 'Debian' if r[:variant].nil?
elsif File.exists?('/etc/redhat-release') or File.exists?('/etc/centos-release')
r[:family] = 'RedHat' if r[:family].nil?
r[:distro] = 'CentOS' if File.exists?('/etc/centos-release')
elsif File.exists?('/etc/SuSE-release')
r[:distro] = 'SLES' if r[:distro].nil?
end
return r
end这不是一个处理地球上每个GNU/Linux发行版的完整解决方案。其实远不是这样的。例如,它不区分OpenSUSE和,尽管它们是两个完全不同的野兽。此外,这是一个相当大的意大利面,即使只有几个发行版。但它可能是一个人可以建立起来的东西。
发布于 2019-03-10 11:25:28
Linux发行版是一组软件,通常由它们的包管理器、窗口系统、窗口管理器和桌面环境来区分。那是很多可互换的部分。如果系统保留了包管理器,但更改了窗口系统和桌面环境,我们是否称它为新发行版?没有明确的答案,所以各种工具会给出稍微不同的答案。
火车有一个分布族的整体层次,可能是其中最复杂的。列车与欧海就在这里的快速比较。它被设计为在网络连接上运行,但在本地运行也很好,如下所示:
# gem install train
Train.create('local').connection.os[:name] #=> eg. "centos", "linuxmint"
Train.create('local').connection.os[:family] #=> eg. "redhat", "debian"法特的[医]家庭事实返回,如Ubuntu的"Debian“。对于Facter,检索事实的一般形式是Facter[factname].value。
# gem install facter
require 'facter'
puts Facter['osfamily'].value奥海的platform事实返回,如Ubuntu的"debian“和CentOS的“流变”。对于Ohai,检索事实的一般形式是node[factname]。
# gem install ohai
node['platform'] #=> eg. "ubuntu" or "mint"
node['platform_family'] #=> eg. "debian" for Ubuntu and Mint无法区分平台的Ruby系统信息库
平台检索一些基本数据,并能很好地区分各种Unix平台。但是,它根本不处理Linux的不同发行版。Platform::IMPL将返回:freebsd、:netbsd、:hpux等,但是所有的Linux发行版都是:linux。sys-uname和系统信息是相似的。实用林佛甚至更基本,在任何系统上都会失败,而不是Windows、Mac和Linux。
发布于 2019-03-08 12:00:53
require 'facter'
puts Facter['osfamily'].valuehttps://stackoverflow.com/questions/25970280
复制相似问题