Files
bash-helper/helper
2025-09-01 12:55:44 +02:00

272 lines
6.7 KiB
Plaintext

#####################################################################
# First Created: 15082025 Author: Allan Desc:
# supplies multiple functions that can help in creating bash scripts.
#
# For documentation and examples please see this link.
#
# https://wiki.x-files.dk/doku.php?id=linux:bash_helper_howto
#
# Download from git use this link.
#
# https://git.x-files.dk/bash/bash-helper-script
#
#####################################################################
#
# Are we root if not die
#
must_be_root() {
local required="$1"
if [[ "$required" == "yes" ]]; then
if [[ $UID -ne 0 ]]; then
printf "\nThis script must be run as root or use sudo\n\n"
exit 1
fi
fi
}
#
# Are we in the right directory if not die
#
must_be_in_dir() {
local required_dir="$1"
if [[ "${PWD##*/}" != "$required_dir" ]]; then
printf "\nWrong directory! This script must be run from the directory '%s'\n\n" "$required_dir"
exit 1
fi
}
#
# Output x characters
#
line() { printf "%0.s$1" $(seq 1 $2); printf '\n'; }
#
# 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
#
must_be_running() {
local bad_services=()
for service in "$@"; do
if [[ "$(systemctl is-active "$service")" != "active" ]]; then
bad_services+=("$service")
fi
done
if (( ${#bad_services[@]} > 0 )); then
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
}
#
# Service(s) must not be running
#
must_not_be_running() {
local bad_services=()
for service in "$@"; do
if [[ "$(systemctl is-active "$service")" == "active" ]]; then
bad_services+=("$service")
fi
done
if (( ${#bad_services[@]} > 0 )); then
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 being used.
#
detect_mode() {
for arg in "$@"; do
if [[ "$arg" == -* ]]; then
echo "flags"
return
fi
done
echo "positional"
}
#
# Print usage dynamically (adapts to mode)
#
usage() {
local mode
mode=$(detect_mode "$@")
printf "Usage: %s " "$0"
if [[ $mode == "flags" ]]; then
printf "[options]\n"
for flag in "${flags[@]}"; do
short="${flag%%:*}"
rest="${flag#*:}"
desc="${rest#*:}"
desc="${desc%%:*}"
printf " -%s %s\n" "$short" "$desc"
done
printf " -h Show help\n\n"
printf "Example:\n"
example="$0"
for flag in "${flags[@]}"; do
short="${flag%%:*}"
rest="${flag#*:}" # remove short
rest="${rest#*:}" # remove var
desc="${rest%%:*}" # description
rest="${rest#*:}"
sample="${rest%%:*}"
example+=" -$short ${sample:-$desc}"
done
printf "%s\n\n" "$example"
else
# positional usage
for flag in "${flags[@]}"; do
rest="${flag#*:}"
var="${rest%%:*}"
rest="${rest#*:}"
desc="${rest%%:*}"
rest="${rest#*:}"
sample="${rest%%:*}"
required="${flag##*:}"
if [[ "$required" == "true" ]]; then
printf "<%s> " "$desc"
else
printf "[%s] " "$desc"
fi
done
printf "\n\n"
printf "Example:\n"
example="$0"
for flag in "${flags[@]}"; do
rest="${flag#*:}" # remove short
rest="${rest#*:}" # remove var
desc="${rest%%:*}"
rest="${rest#*:}"
sample="${rest%%:*}"
example+=" ${sample:-$desc}"
done
printf "%s\n\n" "$example"
fi
}
#
# Validate required input (works in both modes)
#
validate_input() {
local mode
mode=$(detect_mode "$@")
local missing=false
local i=1
for flag in "${flags[@]}"; do
short="${flag%%:*}"
rest="${flag#*:}"
var="${rest%%:*}"
rest="${rest#*:}"
desc="${rest%%:*}"
rest="${rest#*:}"
sample="${rest%%:*}"
required="${flag##*:}"
value="${!var}"
if [[ -z "$value" && $mode == "positional" ]]; then
value="${!i}"
if [[ -n "$value" ]]; then
printf -v "$var" '%s' "$value"
fi
fi
if [[ "$required" == "true" && -z "${!var}" ]]; then
printf "Error! %s is empty.\n" "$desc"
missing=true
fi
((i++))
done
if [[ $missing == true ]]; then
printf "\n"
usage "$@"
exit 1
fi
}
#
# git clone if repo does not exist
#
git_clone_if_missing() {
local repo="$1"
local target_dir="$2"
[[ -d "$target_dir/.git" ]] && return
echo "Cloning repository $repo into $target_dir..."
if ! git clone --quiet --branch main "$repo" "$target_dir"; then
echo "Error: Failed to clone repository $repo"
exit 1
fi
}
#
# Creates a MariaDB database and user for both localhost and 127.0.0.1
#
setup_mariadb() {
local mariapwd="$1" dbname="$2" dbuser="$3" dbpass="$4"
if [[ -z "$mariapwd" || -z "$dbname" || -z "$dbuser" || -z "$dbpass" ]]; then
echo "Error: setup_mariadb requires mariapwd, dbname, dbuser, dbpass"
exit 1
fi
# Create a temporary MySQL defaults file
local tmpfile
tmpfile=$(mktemp)
cat >"$tmpfile" <<EOF
[client]
user=root
password=$mariapwd
EOF
# Execute the SQL safely using the temporary file
mysql --defaults-extra-file="$tmpfile" <<EOF
CREATE DATABASE IF NOT EXISTS \`$dbname\`;
CREATE USER IF NOT EXISTS '$dbuser'@'localhost' IDENTIFIED BY '$dbpass';
CREATE USER IF NOT EXISTS '$dbuser'@'127.0.0.1' IDENTIFIED BY '$dbpass';
GRANT ALL PRIVILEGES ON \`$dbname\`.* TO '$dbuser'@'localhost';
GRANT ALL PRIVILEGES ON \`$dbname\`.* TO '$dbuser'@'127.0.0.1';
FLUSH PRIVILEGES;
EOF
# Remove the temporary file
rm -f "$tmpfile"
}