bash命令中redhat 与 debian系统的识别

如何在bash 脚本识别redHat和debian环境?

阅读 4.5k
3 个回答

参考一下How can I get distribution name and version number in a simple shell script?

其中被采纳的答案

ARCH=$(uname -m | sed 's/x86_//;s/i[3-6]86/32/')

if [ -f /etc/lsb-release ]; then
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
    OS=Debian  # XXX or Ubuntu??
    VER=$(cat /etc/debian_version)
elif [ -f /etc/redhat-release ]; then
    # TODO add code for Red Hat and CentOS here
    ...
else
    OS=$(uname -s)
    VER=$(uname -r)
fi

稍微改改这段代码就可以了

如果系统可能性只有redhat和debian的话,可以执行yum --version然后判断执行结果。yum执行成功的话就redhar,不然就得采用上面那位同学的方法了。

look at this:

#!/bin/bash

DEBIAN='Debian'
REDHAT='rhel'

function os_type() {
    OS=$(lsb_release -ds 2>/dev/null || cat /etc/*release 2>/dev/null | head -n1 || uname -om)    
    if [[ $OS == *Debian* ]]; then
        OS_TYPE='Debian'
        echo "Debian  GNU/Linux distributions"
    elif [[ $OS == *rhel* ]]; then
        OS_TYPE='Redhat'
        echo "Redhat GNU/Linux distributions"
    else
        echo "$OS"
    fi

}

os_type
echo "$OS_TYPE"
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题