首页 > 系统相关 >如何查看Linux发行版本是Ubuntu还是CentOS

如何查看Linux发行版本是Ubuntu还是CentOS

时间:2023-04-12 09:47:31浏览次数:47  
标签:lsb CentOS Linux echo etc Ubuntu release

 

https://askubuntu.com/questions/459402/how-to-know-if-the-running-platform-is-ubuntu-or-centos-with-help-of-a-bash-scri

 

How to know if the running platform is Ubuntu or CentOS with help of a Bash script?

 

OS_NAME=$(lsb_release -si) 
case "$OS_NAME" in
    CentOS)
    echo CentOS
        ;;
    Ubuntu)
    echo Ubuntu
        ;;
    *)
    echo Others
        ;;
esac

 

 

 

  1. Use /etc/os-release

    awk -F= '/^NAME/{print $2}' /etc/os-release
    
  2. Use the lsb_release tools if available

    lsb_release -d | awk -F"\t" '{print $2}'
    
  3. Use a more complex script that should work for the great majority of distros:

    # Determine OS platform
    UNAME=$(uname | tr "[:upper:]" "[:lower:]")
    # If Linux, try to determine specific distribution
    if [ "$UNAME" == "linux" ]; then
        # If available, use LSB to identify distribution
        if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
            export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
        # Otherwise, use release info file
        else
            export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
        fi
    fi
    # For everything else (or if above failed), just use generic identifier
    [ "$DISTRO" == "" ] && export DISTRO=$UNAME
    unset UNAME

 

 

 

The lsb_release command was added to the Linux Standard Base (ISO/IEC 23360) for this purpose:

$ lsb_release -si
Ubuntu
$ lsb_release -sd
Ubuntu 18.04.3 LTS
$ lsb_release -sr
18.04
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.3 LTS
Release:        18.04
Codename:       bionic

Therefore a case statement along the lines of

case "`/usr/bin/lsb_release -si`" in
  Ubuntu) echo 'This is Ubuntu Linux' ;;
       *) echo 'This is something else' ;; 
esac

should do what you want.

On newer Linux distributions based on systemd there is also /etc/os-release, which is intended to be included into shell scripts with the source (.) command, as in

. /etc/os-release

case "$ID" in
  ubuntu) echo 'This is Ubuntu Linux' ;;
       *) echo 'This is something else' ;; 
esac

But in the use-case example you gave, you may actually be more interested not in the name of the distribution, but whether it has apt-get or yum. You could just test for the presence of the files /usr/bin/apt-get or /usr/bin/yum with if [ -x /usr/bin/apt-get ]; then ... or for the presence of associated infrastructure directories, such as /var/lib/apt and /etc/apt/.

 

标签:lsb,CentOS,Linux,echo,etc,Ubuntu,release
From: https://www.cnblogs.com/sinferwu/p/17308693.html

相关文章

  • linux安装两个python版本
    1.下载python3安装包wgethttps://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz2.解压python的tgz压缩包文件tar-xzvfPython-3.7.2.tgz3.进入解压的文件cdPython-3.7.24.在python文件路径下编译pythonprefix=/usr/local/python37,指定python安装路径,这个路径......
  • linux 系统巡检脚本
    #!/bin/bashreadkeyecho"警告:本脚本只是一个检查的操作,未对服务器做任何修改,管理员可以根据此报告进行相应的设置。"echo---------------------------------------主机安全检查-----------------------echo"系统版本"uname-aecho-----------------------------------......
  • linux系统查看ip的命令
    1、ipa2、ifconfig3、hostname-i......
  • Linux 运维必须掌握150个命令讲解自我总结版本
    Linux运维必须掌握150个命令讲解https://wangchujiang.com/linux-command/apt-getlinux命令在线中⽂⼿册(51yip.com)explainshell.com-find(1)-searchforfilesinadirectoryhierarchy1.线上查询及帮助命令(3个)manhelpInfos ⽂件和⽬录操作命令(19个)l......
  • Linux VS Powershell by ChatGPT
    CommandLinuxExamplePowerShellExampledstatdstat-taGet-Counter'\Processor(_Total)%ProcessorTime'sarsar-u110Get-Counter'\Processor(_Total)%ProcessorTime'slurmsbatchscript.shStart-Processpowershell.exe-A......
  • Linux ls 命令
    Linuxls命令ls-alih这个是非常重要的命令,就算不做运维也必须熟练[root@VM-12-14-centoslocal]#ls-alihtotal52K3520drwxr-xr-x.13rootroot4.0KNov52019.14drwxr-xr-x.14rootroot4.0KJan82021..3521drwxr-xr-x.2rootroot4.0K......
  • 高并发下的linux优化
    每日一问题探索-高并发下的linux优化原创 五分钟学SRE 五分钟学SRE 2023-02-2523:42 发表于香港收录于合集#每日一问题探索10个#五分钟学SRE51个 针对高并发服务,对Linux内核和网络进行优化可以提高系统的性能和稳定性。本文将深入探讨如何对Linux内核和......
  • Linux搭建docker
    ###1.查看系统的内核版本```bash[root@wide~]#uname-r3.10.0-1160.el7.x86_64```###2.将yum更新到最新版本```bash[root@wide~]#yumupate-yComplete!```###3.安装Docker所需的依赖包```bash[root@wide~]#sudoyuminstall-yyum-utilsdevice-mapper-persist......
  • Linux umount 命令
    Linuxumount命令Linuxumount(英文全拼:unmount)命令用于卸除文件系统。umount可卸除目前挂在Linux目录中的文件系统。语法umount[-ahnrvV][-t<文件系统类型>][文件系统]参数:-a卸除/etc/mtab中记录的所有文件系统。-h显示帮助。-n卸除时不要将信息存入/etc/mtab文件......
  • Code-C++-Linux-统计一个文件夹占据空间大小
    Code-C++-Linux-统计一个文件夹占据空间大小https://my.oschina.net/Tsybius2014/blog/330628从以上链接中拷贝的代码#include<stdio.h>#include<sys/stat.h>#include<sys/types.h>#include<unistd.h>#include<stdlib.h>#include<dirent.h>#incl......