![如何使用 Bash 确定系统是 RHEL、CentOS 还是 Oracle Linux?](https://linux22.com/image/14263/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20Bash%20%E7%A1%AE%E5%AE%9A%E7%B3%BB%E7%BB%9F%E6%98%AF%20RHEL%E3%80%81CentOS%20%E8%BF%98%E6%98%AF%20Oracle%20Linux%EF%BC%9F.png)
我正在创建一个脚本来创建必要的文件并安装必要的软件包,以将我的新系统从 CentOS/Oracle Linux 迁移到 RHEL 8。我想检测运行该脚本的系统是使用 CentOS 8 还是 Oracle Linux 8,并阻止任何使用 RHEL 8 运行该脚本的系统(作为保护措施)。如何使用 Bash 执行此操作?应该检查哪些文件,或者应该使用哪些命令来确定系统是 RHEL 还是 CentOS?
答案1
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