From c71ce67a5a847444c0375faae68f734a490e15d8 Mon Sep 17 00:00:00 2001 From: Martin Berghaus Date: Sun, 23 Feb 2025 00:07:07 +0100 Subject: [PATCH] much better --- core/printOwnDomain.sh | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/core/printOwnDomain.sh b/core/printOwnDomain.sh index 5d619ef..18876a0 100644 --- a/core/printOwnDomain.sh +++ b/core/printOwnDomain.sh @@ -11,18 +11,21 @@ _CORE_SCRIPTS="$(dirname ${_SCRIPT:?"Missing SCRIPT"} 2> /dev/null)/" _CIS_ROOT="$(dirname ${_CORE_SCRIPTS:?"Missing CORE_SCRIPTS"} 2> /dev/null)/" _OVERRIDE_DOMAIN_FILE="${_CIS_ROOT:?"Missing CIS_ROOT"}overrideOwnDomain" -# Wenn OVERRIDING_DOMAIN_FILE enhält lesbare Daten -grep '[^[:space:]]' "${_OVERRIDE_DOMAIN_FILE:?"Missing OVERRIDE_DOMAIN_FILE"}" &> /dev/null \ - && echo "WARNING: Domain has been overridden by: ${_OVERRIDE_DOMAIN_FILE}" > /dev/stderr \ - && cat "${_OVERRIDE_DOMAIN_FILE}" \ - && exit 0 - -_BOOT_HOSTNAME="$(hostname -b)" # There has to be one dot at least. -echo "${_BOOT_HOSTNAME}" | grep -v '\.' &> /dev/null \ - && echo "It was impossible to find out the domain of this host, please prepare this host first." > /dev/stderr \ - && exit 1 +_BOOT_DOMAIN="$(hostname -b | grep -F '.' | cut -d. -f2-)" -#Removes shortest matching pattern '*.' from the begin to get the domain -echo "${_BOOT_HOSTNAME#*.}" \ +# Take OVERRIDING_DOMAIN_FILE without empty lines and comments, then take the first line without leading spaces +_OVERRIDE_DOMAIN="$(grep -vE '^[[:space:]]*$|^[[:space:]]*#' "${_OVERRIDE_DOMAIN_FILE}" 2> /dev/null | head -n 1 | xargs)" + +! [ -z "${_OVERRIDE_DOMAIN}" ] \ + && [ "${_OVERRIDE_DOMAIN}" != "${_BOOT_DOMAIN}" ] \ + && echo "WARNING: Domain has been overridden by: ${_OVERRIDE_DOMAIN_FILE}" > /dev/stderr \ + && echo "${_OVERRIDE_DOMAIN}" \ && exit 0 + +! [ -z "${_BOOT_DOMAIN}" ] \ + && echo "${_BOOT_DOMAIN}" \ + && exit 0 + +echo "It was impossible to find out the domain of this host, please prepare this host first." > /dev/stderr +exit 1