我可以通过多种方式检查运行 Ruby 代码的平台的操作系统:
-
RUBY_PLATFORM
: https://stackoverflow.com/a/171011/462015 https://stackoverflow.com/a/171011/462015
-
RbConfig::CONFIG['host_os']
: https://stackoverflow.com/a/13586108/462015 https://stackoverflow.com/a/13586108/462015
是否可以知道正在运行的 Linux 发行版?例如基于 Debian 或基于 Red Hat 的发行版。
正如上面评论部分所指出的,似乎没有确定的“在每个发行版中都有效”的方法来做到这一点。以下是我用来检测脚本正在运行的环境类型的方法:
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 和 SUSE Linux Enterprise Server 没有区别,尽管它们是两种完全不同的野兽。此外,即使只有几个发行版,它也是意大利面条。但这可能是人们可以在此基础上继续发展的东西。
您可以从以下位置找到更完整的分布检测示例源代码 https://github.com/puppetlabs/facter of Facter https://puppetlabs.com/blog/facter-part-1-facter-101除其他外,它用于将事实提供给配置管理系统Puppet https://github.com/puppetlabs/puppet.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)