bash 用于查找机器操作系统的 Shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35236947/
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 14:12:35  来源:igfitidea点击:

Shell Script to find the Operating System of the machine

bashshelloperating-system

提问by ssk

How to find the operating system using bash script? I found this answer: Detect the OS from a Bash script. It is not clear it would work on Mac OS X.

如何使用bash脚本查找操作系统?我找到了这个答案:Detect the OS from a Bash script。目前尚不清楚它是否适用于 Mac OS X。

I would like to find it on Mac OS X vs different linux OS's.

我想在 Mac OS X 和不同的 linux 操作系统上找到它。

回答by nclandrei

For Linux you can type in the following bash command:

对于 Linux,您可以输入以下 bash 命令:

$ cat /etc/*-release

For Mac OS X you can try one of these commands:

对于 Mac OS X,您可以尝试以下命令之一:

$ sw_vers -productVersion 
$ system_profiler SPSoftwareDataType

回答by ssk

Derived from other answers, this worked for me:

来自其他答案,这对我有用:

CURRENT_OS="OSX" #CENTOS, UBUNUTU are other valid options
function findCurrentOSType()
{
    echo "Finding the current os type"
    echo
    osType=$(uname)
    case "$osType" in
            "Darwin")
            {
                echo "Running on Mac OSX."
                CURRENT_OS="OSX"
            } ;;    
            "Linux")
            {
                # If available, use LSB to identify distribution
                if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
                    DISTRO=$(gawk -F= '/^NAME/{print }' /etc/os-release)
                else
                    DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
                fi
                CURRENT_OS=$(echo $DISTRO | tr 'a-z' 'A-Z')
            } ;;
            *) 
            {
                echo "Unsupported OS, exiting"
                exit
            } ;;
    esac
}

回答by Nic Wanavit

use uname

使用 uname

$(uname -s)

this will give you the os name (Darwin = OSX)

这将为您提供操作系统名称(Darwin = OSX)

$(uname -v)

will give you the os version

会给你 os 版本

see uname manual here

在此处查看 uname 手册