如何在bash 脚本识别redHat和debian环境?
如果系统可能性只有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"
7 回答5.6k 阅读
4 回答4.2k 阅读
2 回答966 阅读✓ 已解决
2 回答3.5k 阅读
1 回答1.2k 阅读✓ 已解决
2 回答1.4k 阅读✓ 已解决
1 回答573 阅读✓ 已解决
参考一下How can I get distribution name and version number in a simple shell script?
其中被采纳的答案
稍微改改这段代码就可以了