【问题标题】:How Do I Determine the Distribution and Version of Linux Using PowerShell 6.x or Newer?如何使用 PowerShell 6.x 或更高版本确定 Linux 的分发和版本?
【发布时间】:2020-04-18 19:05:11
【问题描述】:

使用 PowerShell 6.x 或更高版本中的 PowerShell 脚本,在未知的 Linux 发行版上运行,我如何以编程方式可靠地确定操作系统的发行版和版本号?

编辑: 也许像这个答案,加上一些字符串解析?: https://unix.stackexchange.com/a/6348

【问题讨论】:

  • 如果lsb_release 程序可用,您应该使用它,否则使用/etc/os-release 的内容。你如何在 PowerShell 中做到这一点超出了我的范围。请注意,版本不必是数字,例如用于 Debian 测试和不稳定。
  • 什么linux发行版?如果使用 LSB(Linux 标准库),您可以使用 bash -c "lsb_release -a" 在 powershell 中运行 bash。否则你可以运行bash -c "cat /etc/*-release"

标签: linux powershell powershell-core


【解决方案1】:

我也需要在 powershell 脚本中执行此操作。我使用以下解决方案:

$distribution = Invoke-Expression "lsb_release -cs"

这使得$distribution 包含例如以下值:

bionic

你可以对版本做同样的事情:

$version = Invoke-Expression "lsb_release -rs"

这使得$version 包含例如以下值:

18.04

【讨论】:

    【解决方案2】:

    这应该会为您提供发行版和版本号。

    $release = Invoke-Expression "(lsb_release -ds || cat /etc/*release || uname -om) 2>/dev/null | head -n1"
    

    $release 将包含 Ubuntu 18.04.5 LTSCentOS Linux release 7.9.2009 (Core) 之类的内容。

    如果您像我一样只需要发行版,则可以使用 Regex 进行捕获。

    $release | grep -Po '^[^\s]+'
    

    $release 将包含 UbuntuCentOS 之类的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多