You've already forked bash-helper
logic changes
This commit is contained in:
45
helper
45
helper
@@ -11,7 +11,7 @@
|
|||||||
# https://git.x-files.dk/bash/bash-helper-script
|
# https://git.x-files.dk/bash/bash-helper-script
|
||||||
#
|
#
|
||||||
#####################################################################
|
#####################################################################
|
||||||
|
|
||||||
#
|
#
|
||||||
# Are we root if not die
|
# Are we root if not die
|
||||||
#
|
#
|
||||||
@@ -41,10 +41,17 @@ must_be_in_dir() {
|
|||||||
# Output x characters
|
# Output x characters
|
||||||
#
|
#
|
||||||
line() { printf "%0.s$1" $(seq 1 $2); printf '\n'; }
|
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
|
# Service(s) must be running
|
||||||
@@ -58,10 +65,13 @@ must_be_running() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
if (( ${#bad_services[@]} > 0 )); then
|
if (( ${#bad_services[@]} > 0 )); then
|
||||||
echo "The following service(s) appear not to be running, cannot continue..."
|
local services_cap
|
||||||
for svc in "${bad_services[@]}"; do
|
services_cap=$(_capitalize_services "${bad_services[@]}")
|
||||||
echo " - $svc"
|
if (( ${#bad_services[@]} == 1 )); then
|
||||||
done
|
echo "The service $services_cap is not running! Cannot continue..."
|
||||||
|
else
|
||||||
|
echo "The services $services_cap are not running! Cannot continue..."
|
||||||
|
fi
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@@ -78,16 +88,19 @@ must_not_be_running() {
|
|||||||
done
|
done
|
||||||
|
|
||||||
if (( ${#bad_services[@]} > 0 )); then
|
if (( ${#bad_services[@]} > 0 )); then
|
||||||
echo "The following service(s) appear already to be running, cannot continue..."
|
local services_cap
|
||||||
for svc in "${bad_services[@]}"; do
|
services_cap=$(_capitalize_services "${bad_services[@]}")
|
||||||
echo " - $svc"
|
if (( ${#bad_services[@]} == 1 )); then
|
||||||
done
|
echo "The service $services_cap is already running! Cannot continue..."
|
||||||
|
else
|
||||||
|
echo "The services $services_cap are already running! Cannot continue..."
|
||||||
|
fi
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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() {
|
detect_mode() {
|
||||||
for arg in "$@"; do
|
for arg in "$@"; do
|
||||||
@@ -98,7 +111,7 @@ detect_mode() {
|
|||||||
done
|
done
|
||||||
echo "positional"
|
echo "positional"
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
# Print usage dynamically (adapts to mode)
|
# Print usage dynamically (adapts to mode)
|
||||||
#
|
#
|
||||||
@@ -164,7 +177,7 @@ usage() {
|
|||||||
printf "%s\n\n" "$example"
|
printf "%s\n\n" "$example"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
#
|
#
|
||||||
# Validate required input (works in both modes)
|
# Validate required input (works in both modes)
|
||||||
#
|
#
|
||||||
|
Reference in New Issue
Block a user