logic changes

This commit is contained in:
2025-08-26 09:58:22 +02:00
parent bc53a6c0dd
commit 9e78e1acb2

45
helper
View File

@@ -11,7 +11,7 @@
# https://git.x-files.dk/bash/bash-helper-script
#
#####################################################################
#
# Are we root if not die
#
@@ -41,10 +41,17 @@ must_be_in_dir() {
# Output x characters
#
line() { printf "%0.s$1" $(seq 1 $2); printf '\n'; }
#
# Checks if a service is running or not and react accordingly
#
# Capitalize first letter of each service name
#
_capitalize_services() {
local result=()
for svc in "$@"; do
result+=("$(echo "$svc" | awk '{print toupper(substr($0,1,1)) tolower(substr($0,2))}')")
done
echo "${result[*]}"
}
#
# Service(s) must be running
@@ -58,10 +65,13 @@ must_be_running() {
done
if (( ${#bad_services[@]} > 0 )); then
echo "The following service(s) appear not to be running, cannot continue..."
for svc in "${bad_services[@]}"; do
echo " - $svc"
done
local services_cap
services_cap=$(_capitalize_services "${bad_services[@]}")
if (( ${#bad_services[@]} == 1 )); then
echo "The service $services_cap is not running! Cannot continue..."
else
echo "The services $services_cap are not running! Cannot continue..."
fi
exit 1
fi
}
@@ -78,16 +88,19 @@ must_not_be_running() {
done
if (( ${#bad_services[@]} > 0 )); then
echo "The following service(s) appear already to be running, cannot continue..."
for svc in "${bad_services[@]}"; do
echo " - $svc"
done
local services_cap
services_cap=$(_capitalize_services "${bad_services[@]}")
if (( ${#bad_services[@]} == 1 )); then
echo "The service $services_cap is already running! Cannot continue..."
else
echo "The services $services_cap are already running! Cannot continue..."
fi
exit 1
fi
}
#
# Detect input mode: "flags" or "positional" this will detect whether $1 or getopts is beeing used.
# Detect input mode: "flags" or "positional" this will detect whether $1 or getopts is being used.
#
detect_mode() {
for arg in "$@"; do
@@ -98,7 +111,7 @@ detect_mode() {
done
echo "positional"
}
#
# Print usage dynamically (adapts to mode)
#
@@ -164,7 +177,7 @@ usage() {
printf "%s\n\n" "$example"
fi
}
#
# Validate required input (works in both modes)
#