From 9e78e1acb26b024d7c1892ad8a4ec8fc1b62aa06 Mon Sep 17 00:00:00 2001 From: allan Date: Tue, 26 Aug 2025 09:58:22 +0200 Subject: [PATCH] logic changes --- helper | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/helper b/helper index 87e5775..38b2547 100644 --- a/helper +++ b/helper @@ -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) #