【问题标题】:How to detect OS system using BASH?如何使用 BASH 检测 OS 系统?
【发布时间】:2023-01-17 21:08:00
【问题描述】:

该脚本应该能够检测到正在运行的操作系统。 可供选择的操作系统是 Arch Linux、Centos 和 Ubuntu。

os=$(uname) 
if [ "$os" == "Arch" ]; then   
  echo "Arch Linux detected" 
elif [ "$os" == "CentOS" ]; then   
  echo "CentOS detected" 
elif [ "$os" == "Ubuntu" ]; then   
  echo "Ubuntu detected" 
else   
  echo "Unknown OS detected"
fi```

Output: Unknown OS detected


I tried doing this:

\`del1()

{
os=$(cat /etc/os-release | grep "PRETTY_NAME")
}
del1

echo "The operating system is: $os"\`

The output: PRETTY_NAME="Ubuntu 20.04.2 LTS"

But I want to check between Centos, Arch Linux and Ubuntu.

Any suggestions?

【问题讨论】:

    标签: bash ubuntu linux-kernel bash-completion


    【解决方案1】:

    uname 命令在 Linux 上运行时将始终返回 Linux,所以当然这永远不会起作用。

    使用/etc/os-release可能是最好的解决方案,但不要grep它以获取信息;该文件是 shell 变量的集合,您可以使用 . 命令获取这些变量,因此您可以这样写:

    #!/bin/sh
    
    . /etc/os-release
    
    case $ID in
      ubuntu) echo "This is Ubuntu!"
        ;;
    
      arch) echo "This is Arch Linux!"
        ;;
    
      centos) echo "This is CentOS!"
        ;;
    
      *) echo "This is an unknown distribution."
          ;;
    esac
    

    【讨论】:

    • 这是最干净的! man os-release 有关变量的详细信息。变量$ID 是最好的选择。 /etc/os-release 文件被所有使用 systemd 的 linux 发行版采用。这里有更多细节:0pointer.de/blog/projects/os-release
    【解决方案2】:

    您可以使用 grep 命令过滤 cat /etc/os-release 命令的输出以获取指示操作系统的特定字符串。 例如,您可以使用以下命令来检查 Ubuntu:

    os=$(cat /etc/os-release | grep -o "Ubuntu")
    

    然后您可以使用 if 语句来检查变量是否等于 Ubuntu:

    if [ "$os" == "Ubuntu" ]; then   
      echo "Ubuntu detected" 
    else   
      echo "Not Ubuntu detected"
    fi
    

    您可以执行相同的操作来检查 Arch Linux:

    os=$(cat /etc/os-release | grep -o "Arch")
    

    对于 Centos:

    os=$(cat /etc/os-release | grep -o "CentOS")
    

    您还可以使用 cat /etc/*-release 而不是 cat /etc/os-release 来更全面地检测操作系统。

    您还可以使用lsb_release -a 命令来获取有关操作系统的分布和版本的更多详细信息。

    os=$(lsb_release -a | grep -o "Ubuntu")
    

    然后,您可以创建一个函数来逐个检查每个操作系统并相应地打印输出。

    check_os(){
    os=$(cat /etc/os-release | grep -o "Ubuntu")
    if [ "$os" == "Ubuntu" ]; then   
      echo "Ubuntu detected" 
    else
      os=$(cat /etc/os-release | grep -o "Arch")
      if [ "$os" == "Arch" ]; then   
        echo "Arch Linux detected" 
      else
        os=$(cat /etc/os-release | grep -o "CentOS")
        if [ "$os" == "CentOS" ]; then   
          echo "CentOS detected" 
        else   
          echo "Unknown OS detected"
        fi
      fi
    fi
    }
    check_os
    

    请注意,这种方法可能不是 100% 准确,最好使用适当的包管理器命令来检查操作系统版本和分发。

    【讨论】:

    • (a) 你不应该使用grep,并且 (b) 如果你决定使用grep,你不需要使用catgrep -o Ubuntu /etc/os-release
    【解决方案3】:

    如果您只需要检查操作系统的名称,您可以尝试;

    source /etc/os-release
    echo "The operating system is: $NAME"
    

    【讨论】:

      猜你喜欢
      • 2010-09-28
      • 2017-07-30
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 2012-05-18
      • 2011-06-12
      • 1970-01-01
      • 2012-04-01
      相关资源
      最近更新 更多