如何使用 Bash 确定系统是 RHEL、CentOS 还是 Oracle Linux?

如何使用 Bash 确定系统是 RHEL、CentOS 还是 Oracle Linux?

我正在创建一个脚本来创建必要的文件并安装必要的软件包,以将我的新系统从 CentOS/Oracle Linux 迁移到 RHEL 8。我想检测运行该脚本的系统是使用 CentOS 8 还是 Oracle Linux 8,并阻止任何使用 RHEL 8 运行该脚本的系统(作为保护措施)。如何使用 Bash 执行此操作?应该检查哪些文件,或者应该使用哪些命令来确定系统是 RHEL 还是 CentOS?

答案1

https://get.docker.com

get_distribution() {
    lsb_dist=""
    # Every system that we officially support has /etc/os-release
    if [ -r /etc/os-release ]; then
        lsb_dist="$(. /etc/os-release && echo "$ID")"
    fi
    # Returning an empty string here should be alright since the
    # case statements don't act unless you provide an actual value
    echo "$lsb_dist"
}


    # perform some very rudimentary platform detection
    lsb_dist=$( get_distribution )
    lsb_dist="$(echo "$lsb_dist" | tr '[:upper:]' '[:lower:]')"

    case "$lsb_dist" in

        ubuntu)
            if command_exists lsb_release; then
                dist_version="$(lsb_release --codename | cut -f2)"
            fi
            if [ -z "$dist_version" ] && [ -r /etc/lsb-release ]; then
                dist_version="$(. /etc/lsb-release && echo "$DISTRIB_CODENAME")"
            fi
        ;;

        debian|raspbian)
            dist_version="$(sed 's/\/.*//' /etc/debian_version | sed 's/\..*//')"
            case "$dist_version" in
                11)
                    dist_version="bullseye"
                ;;
                10)
                    dist_version="buster"
                ;;
                9)
                    dist_version="stretch"
                ;;
                8)
                    dist_version="jessie"
                ;;
            esac
        ;;

        centos|rhel|sles)
            if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
                dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
            fi
        ;;

        *)
            if command_exists lsb_release; then
                dist_version="$(lsb_release --release | cut -f2)"
            fi
            if [ -z "$dist_version" ] && [ -r /etc/os-release ]; then
                dist_version="$(. /etc/os-release && echo "$VERSION_ID")"
            fi
        ;;

    esac

相关内容