Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex 如何使用shell脚本查找Linux发行版名称?_Regex_Linux_Bash_Shell_Operating System - Fatal编程技术网

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

Regex 如何使用shell脚本查找Linux发行版名称?,regex,linux,bash,shell,operating-system,Regex,Linux,Bash,Shell,Operating System,我正在编写一个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 怎么可能呢?在

我正在编写一个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
怎么可能呢?在
lsb_release-a
命令或其他东西上应用正则表达式


感谢..

对于几乎所有的linux发行版,
cat/etc/issue
将起到作用

$ lsb_release -i
Distributor ID: Fedora
$ lsb_release -i | cut -f 2-
Fedora
编辑:显然,没有任何解决方案可以应用于所有发行版,因为发行版可以自由地做他们想做的事情


进一步澄清:这并不能保证有效——什么都不是——但根据我的经验,这是最常用的方法。事实上,这是唯一一种工作一致的方法(
lsb_release
,这里提到过,它经常生成
未找到的命令
)。

我会使用
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

您可以从lsb_发行版获取信息:

echo "$(lsb_release -is)"
i
代表分销商id

s
代表缩写

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

还有其他选择:

-r:释放

-d:描述

-c:代号

-a:好的

您可以通过运行
lsb\u release--help
man lsb\u release

来获取此信息,请尝试以下方法:

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

我得到的是:

#!/bin/bash

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

check_arch=$(uname -m)

echo "[$green+$txtrst] Distribution Name: $dist"
#/bin/bash
地区=$(tr-s'\011'\012'
Yup-
lsb发行版
绝对是最健壮、最通用的方法。例如:
lsb_release-d | awk'{print$2}'
。或
lsb_release-ds
lsb_release在CENTOS7上似乎不可用,开箱即用。/etc/issue并非用于存储有关操作系统的信息,可能包含管理员喜欢的任何文本。我从不相信它能检测操作系统或发行版。具体来说,/etc/issue是getty在控制台登录时显示的文本文件。它不能保证包含任何关于发行版的有意义的信息。通常我会
cat/etc/*ease
,因为
/etc
中有一个
fedora发行版
something发行版
文件。这不是一个可靠的解决方案,因为管理员可以将这些文件放入任何他想要的文件中。但是,
lsb_发行版
在centos 7上不存在。这几乎是完美的,不需要在
awk
之间设置管道,虽然
awk-F'=''''''''''/'/'^ID=/{print tolower($2)}'/etc/*-release
做得很好,对于那些希望检测基于Debian
awk-F'='''/''/^ID'/{print tolower($2)的发行版的人来说也是如此“/etc/*-release2>/dev/null
更适合我的用例。
#!/bin/bash

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

check_arch=$(uname -m)

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