【问题标题】:Detect OS type using Perl使用 Perl 检测操作系统类型
【发布时间】:2013-02-16 16:15:57
【问题描述】:

我想使用 Perl 来检测操作系统类型。

例如:

假设我有一个安装程序,以便了解安装程序需要运行哪些命令。我必须检测安装了什么操作系统,例如 Linux。假设它是Linux。现在是哪种类型的 Linux?

  • Fedora
  • Ubuntu
  • CentOS
  • Debian

我该怎么做?

【问题讨论】:

    标签: perl operating-system


    【解决方案1】:

    第一步是检查$^O变量的输出。

    如果输出例如

    Linux

    您需要更多处理来检测使用的是哪个分布。

    perldoc perlvar

    到目前为止,您可以运行lsb_release -a 来查看使用的是哪个发行版。

    如果此命令不存在,您可以测试以下文件是否存在:

    /etc/debian_version # debian or debian based
    /etc/redhat-release # rpm like distro
    

    其他文件测试示例:Detecting Underlying Linux Distro


    考虑使用xaxes' solution 也使用Linux::Distribution 模块来检查分发。


    如果你想检测包管理器,你可以试试这个方法:

     if ($^O eq "linux") {
         my $pm;
    
         do{
             if (-x qx(type -p $_ | tr -d "\n")) {
                 $pm = $_;
                 last;
             }
         } for qw/apt-get aptitude yum emerge pacman urpmi zypper/;
    
         print $pm;
     }
    

    【讨论】:

    • 应该是$^O(大写字母O),而不是零。
    【解决方案2】:

    根据perlvar$OSNAME$^O 将为您提供操作系统。这也等同于使用$Config{'osname'}(更多信息请参见Config)。 Windows 系统的特别说明:

    在 Windows 平台中,$^O 不是很有帮助:因为它始终是 MSWin32 ,它无法区分 95/98/ME/NT/2000/XP/CE/ 。网。使用Win32::GetOSName()Win32::GetOSVersion()(参见Win32perlport)来区分变体。

    为了获得 Linux 机器的确切平台,您需要使用他回答中提到的 xaxes 之类的模块。

    【讨论】:

    • 例如在windows中,如果我做了$^O并且如果它显示了windows,那么我可以像if it = MSWin32然后像Win32::GetOSVersion()那样做吗?
    • 没错。记得在脚本顶部use Win32
    【解决方案3】:

    另外,您可以使用 Config(它是一个核心模块)。这是一个例子:

    use Config;
    print "$Config{osname}\n";
    print "$Config{archname}\n";
    

    在我的 Mac OS X 上,它会打印:

    darwin
    darwin-2level
    

    【讨论】:

      【解决方案4】:

      要确定您的脚本在哪个操作系统上运行,您可以使用$^O

      print $^O
      

      Linux::Distribution 检查分布:

      use Linux::Distribution qw(distribution_name distribution_version);
      
      my $linux = Linux::Distribution->new;
      if (my $distro = $linux->distribution_name()) {
            my $version = $linux->distribution_version();
            print "you are running $distro, version $version\n";
      } else {
            print "distribution unknown\n";
      }
      

      【讨论】:

      • 是的,但是在那种情况下它的 linux 很棒,但是如何获得确切的名称以知道是否使用 apt-get 或 yum 或?
      • 查看包管理器列表怎么样? foreach $manager (@package_manager) { $check = ` $manager `; } [这里检查 $check 包含什么]
      【解决方案5】:

      这在 Redhat 或 CentOS 上运行良好,可轻松调整到其他人...

      # Test for release file
      my $release_file="";
      if ( -e "/etc/redhat-release" ) {
        $release_file="/etc/redhat-release";
      } elsif ( -e "/etc/centos-release" ) {
        $release_file="/etc/centos-release";
      } elsif ( -e "/etc/system-release" ) {
        $release_file="/etc/system-release";
      } else {
        print "OS Release file missing, can't determine version information";
        exit;
      }
      
      my $OS_NAME=`cat $release_file|cut -d " " -f1`;
      my $OS_MAJOR_VERSION=`sed -rn 's/^[^0-9]*([0-9]+)\\.[0-9]+.*/\\1/p' $release_file`;
      my $OS_MINOR_VERSION=`sed -rn 's/^[^0-9]*[0-9]+.([0-9]+).*/\\1/p' $release_file`;
      
      chomp($OS_NAME);
      chomp($OS_MAJOR_VERSION);
      chomp($OS_MINOR_VERSION);
      
      print "\nOS Name and Version: ${OS_NAME} ${OS_MAJOR_VERSION}.${OS_MINOR_VERSION}\n\n";
      

      【讨论】:

        【解决方案6】:

        你可以检查文件 - /etc/os-release:

        #!/usr/bin/perl
        
        use feature(say);
        use strict;
        use warnings;
        
        
        my %os=();
        
        unless ( open(OS,"cat /etc/os-release|") ){
                say "ErrorOpenPipe OS_release";
                exit;
        }
        
        while (<OS>){
                my @os_param = split /=/, $_;
                $os{$os_param[0]}=$os_param[1];
        }
        
        
        my @FH;
        
        say $os{ID};
        say $os{VERSION_ID};
        

        如果需要,使用其他键:

        $ cat /etc/os-release 
        
        NAME="Ubuntu"
        VERSION="16.04.5 LTS (Xenial Xerus)"
        ID=ubuntu
        ID_LIKE=debian
        PRETTY_NAME="Ubuntu 16.04.5 LTS"
        VERSION_ID="16.04"
        HOME_URL="http://www.ubuntu.com/"
        SUPPORT_URL="http://help.ubuntu.com/"
        BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
        VERSION_CODENAME=xenial
        UBUNTU_CODENAME=xenial
        
        $ cat /etc/os-release 
        
        NAME="CentOS Linux"
        VERSION="7 (Core)"
        ID="centos"
        ID_LIKE="rhel fedora"
        VERSION_ID="7"
        PRETTY_NAME="CentOS Linux 7 (Core)"
        ANSI_COLOR="0;31"
        CPE_NAME="cpe:/o:centos:centos:7"
        HOME_URL="https://www.centos.org/"
        BUG_REPORT_URL="https://bugs.centos.org/"
        
        CENTOS_MANTISBT_PROJECT="CentOS-7"
        CENTOS_MANTISBT_PROJECT_VERSION="7"
        REDHAT_SUPPORT_PRODUCT="centos"
        REDHAT_SUPPORT_PRODUCT_VERSION="7"
        

        【讨论】:

          猜你喜欢
          • 2010-09-24
          • 2014-09-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-03-13
          • 2019-10-04
          • 1970-01-01
          相关资源
          最近更新 更多