RHEL系のOS種類・バージョンを(やんわりと)判定

function rhel_major_version() {
  major_version=$(cat /etc/redhat-release 2> /dev/null | egrep -o "[0-9\.]+" | cut -d "." -f1)
  if [[ $major_version =~ ^[0-9]+$ ]]; then
    echo $major_version
    return 0
  fi
  echo -1
  return 255
}

function is_amazon_linux() {
  is_amazon=$(cat /etc/system-release 2> /dev/null | grep -oi amazon)
  if [ ! -z $is_amazon ]; then
    return 0
  fi
  return 1
}

function is_daemon_installed() {
  if is_amazon_linux || [ `rhel_major_version` -eq 6 ]; then
    test -f "/etc/init.d/$1" > /dev/null
    return $?
  elif [ `rhel_major_version` -eq 7 ]; then
    systemctl list-unit-files | grep $1.service > /dev/null
    return $?
  fi

  echo "failed to detect $1 installation" 1>&2
  return 2
}