bash 如何使用 shell 脚本查找 Linux 发行版名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29581754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:46:54  来源:igfitidea点击:

How to find Linux Distribution name using shell script?

regexlinuxbashshelloperating-system

提问by Pratik Patil

I am writing a shell script in which I need the current operating system name to make it generic. Like:

我正在编写一个 shell 脚本,在该脚本中我需要当前的操作系统名称以使其通用。喜欢:

if [ $Operating_System == "CentOS" ]
then
    echo "CentOS";
    # Do this
elif [ $Operating_System == "Ubuntu" ]
then
    echo "Ubuntu";
    # Do that
else
    echo "Unsupported Operating System";
fi

How will it be possible? Applying regular expression on lsb_release -acommand or something else?

怎么可能?在lsb_release -a命令或其他东西上应用正则表达式?

Thanks..

谢谢..

回答by Ignacio Vazquez-Abrams

$ lsb_release -i
Distributor ID: Fedora
$ lsb_release -i | cut -f 2-
Fedora

回答by Jahid

You can get the info from lsb_release:

您可以从lsb_release以下位置获取信息:

echo "$(lsb_release -is)"

istands for distributor id.

i代表经销商 ID。

sstands for short.

s代表简称。

For ex. It shows Ubuntuinstead of Distributor Id: Ubuntu

例如。它显示Ubuntu而不是Distributor Id: Ubuntu

There are other options:

还有其他选择:

-r : release

-d : description

-c : codename

-a : all

-r : 释放

-d : 描述

-c : 代号

-a : 全部

You can get this information by running lsb_release --helpor man lsb_release

您可以通过运行lsb_release --helpman lsb_release

回答by Filippo Lauria

try this one:

试试这个:

awk '/^ID=/' /etc/*-release | awk -F'=' '{ print tolower() }'

回答by Brian Showalter

DISTRO=$( cat /etc/*-release | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|nameyourdistro)' | uniq )
if [ -z $DISTRO ]; then
    DISTRO='unknown'
fi
echo "Detected Linux distribution: $DISTRO"

回答by b1scoito

Here is what i get:

这是我得到的:

#!/bin/bash

dist=$(tr -s ' 1' '2' < /etc/issue | head -n 1)

check_arch=$(uname -m)

echo "[$green+$txtrst] Distribution Name: $dist"

回答by qwertyboy

For almost all linux distros, cat /etc/issuewill do the trick.

对于几乎所有的 linux 发行版,cat /etc/issue都可以解决问题。

Edit: Obviously, no solution can apply to all distros, as distros are freeto do as they please.

编辑:显然,没有解决方案可以适用于所有发行版,如安装都是免费做的,因为他们请。

Further clarification: This is not guaranteed to work - nothing is - but in my experience, this is the method that most often works. Actually, it's the onlymethod that works consistently (lsb_release, which was mentioned here, often produces command not found).

进一步说明:这不能保证有效 - 没有什么是 - 但根据我的经验,这是最常用的方法。实际上,它是唯一能始终如一地工作的方法(lsb_release此处提到的 ,通常会产生command not found)。

回答by mrflash818

I'd use uname -a

我会用 uname -a

robert@debian:/tmp$ uname -a
Linux debian 3.2.0-4-686-pae #1 SMP Debian 3.2.65-1+deb7u2 i686 GNU/Linux