Skip to content

Commit

Permalink
operating system detection: redhat[678] and get_os
Browse files Browse the repository at this point in the history
 * Tested on CentOS 6 and 7

 * Should be correct for RedHat and CentOS8

Issue #51
  • Loading branch information
sinewalker committed Jul 18, 2019
1 parent 68d5de0 commit e35f0f3
Showing 1 changed file with 38 additions and 11 deletions.
49 changes: 38 additions & 11 deletions bin/dotfiles
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,54 @@ function is_raspbian() {
#lsb_release is not installed by default in Raspbian
[[ -f /etc/rpi-issue ]] || return 1
}
function __redhat_release(){
# sometimes RedHat has lsb_release, and sometimes not
local DIST RELEASE
if type -p lsb_release &> /dev/null; then
DIST=$(lsb_release -i 2> /dev/null)
RELEASE=$(lsb_release -r 2> /dev/null|awk '{print $2}')
elif [[ -f /etc/redhat-release ]]; then
DIST=$(cat /etc/redhat-release)
RELEASE=$(awk '{print $4}' /etc/redhat-release)
else
return 1
fi
echo ${DIST} ${RELEASE}
}
function is_redhat6() {
local FUNCDESC="Return 0 if operating system is a RedHat 6 variant, else return 1"
[[ $(lsb_release -r 2> /dev/null|awk '{print $2}') =~ ^6\. ]] || return 1
local DIST=$(lsb_release -i 2> /dev/null)
[[ ${DIST} =~ Scientific ]] || [[ ${DIST} =~ CentOS ]] \
|| [[ ${DIST} =~ Red ]] || return 1
local DIST_RELEASE=$(__redhat_release) || return 1
local RELEASE=$(echo ${DIST_RELEASE}|awk '{print $NF}')

[[ ${RELEASE} =~ ^6\. ]] || return 1
[[ ${DIST_RELEASE} =~ Scientific ]] || [[ ${DIST_RELEASE} =~ CentOS ]] \
|| [[ ${DIST_RELEASE} =~ Red ]] || return 1
}
function is_redhat7(){
local FUNCDESC="Return 0 if operating system is a RedHat 7 variant, else return 1";
local DIST_RELEASE=$(__redhat_release) || return 1
local RELEASE=$(echo ${DIST_RELEASE}|awk '{print $NF}')

[[ ${RELEASE} =~ ^7\. ]] || return 1
[[ ${DIST_RELEASE} =~ Fedora ]] || [[ ${DIST_RELEASE} =~ CentOS ]] \
|| [[ ${DIST_RELEASE} =~ Red ]] || return 1
}
function is_redhat7() {
local FUNCDESC="Return 0 if operating system is a RedHat 7 variant, else return 1"
local DIST=$(lsb_release -i 2> /dev/null)
[[ ${DIST} =~ Fedora ]] && return 0
[[ $(lsb_release -r 2> /dev/null|awk '{print $2}') =~ ^7\. ]] || return 1
[[ ${DIST} =~ CentOS ]] || [[ ${DIST} =~ Red ]] || return 1
function is_redhat8(){
local FUNCDESC="Return 0 if operating system is a RedHat 8 variant, else return 1";
local DIST_RELEASE=$(__redhat_release) || return 1
local RELEASE=$(echo ${DIST_RELEASE}|awk '{print $NF}')

[[ ${RELEASE} =~ ^8\. ]] || return 1
[[ ${DIST_RELEASE} =~ Fedora ]] || [[ ${DIST_RELEASE} =~ CentOS ]] \
|| [[ ${DIST_RELEASE} =~ Red ]] || return 1
}
function get_os() {
local FUNCDESC="Report operating system. If unknown, prints nothing and returns 1.
If first argument is '1', then list all known OSes that this system is NOT
running."
local RET=1
for OS in osx ubuntu suse raspbian redhat6 redhat7; do
for OS in osx ubuntu suse raspbian redhat6 redhat7 redhat8; do
is_${OS}; [[ ${?} == ${1:-0} ]] && echo ${OS} && RET=0
done
return ${RET}
Expand Down

1 comment on commit e35f0f3

@sinewalker
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue #9

Please sign in to comment.