This page contains some snippets I use as drafts for when doing bash scripts that I publish on the git page.
A function to check weather a service is running or not.
#!/usr/bin/env bash usage() { printf "Usage: %s [options]\n" "$0" printf " -n hostname\n" printf " -m mariadb root password\n" printf " -d wordpress database name\n" printf " -u wordpress database user\n" printf " -p wordpress database password\n" printf " -h Show help\n\n" printf "Example:\n" printf "%s -n wp.something.xyz -m mariadbrootpwd -d wpdbname -u wpusername -p wpdbpassword\n\n" "$0" } # Parse CLI arguments with -m as second option while getopts ":n:m:d:u:p:h" option; do case "$option" in n) hostname="${OPTARG,,}" ;; # lowercase m) mariadbrootpwd="$OPTARG" ;; d) wpdbname="$OPTARG" ;; u) wpdbuser="$OPTARG" ;; p) wpdbpass="$OPTARG" ;; h) usage; exit 0 ;; \?) printf "Invalid option: -%s\n" "$OPTARG"; usage; exit 1 ;; :) printf "Option -%s requires an argument.\n" "$OPTARG"; usage; exit 1 ;; esac done # === Required argument validation === validate_input() { local -a fields=( "hostname:hostname" "mariadb root password:mariadbrootpwd" "wordpress database name:wpdbname" "wordpress database user:wpdbuser" "wordpress database password:wpdbpass" ) local missing=false for field in "${fields[@]}"; do local label="${field%%:*}" local varname="${field##*:}" local value="${!varname}" if [[ -z "$value" ]]; then printf "Error! %s is empty.\n" "$label" missing=true fi done if [[ $missing == true ]]; then printf "\n" usage exit 1 fi } validate_input # Print all variables printf "\nhostname: %s\n" "$hostname" printf "mariadb root password: %s\n" "$mariadbrootpwd" printf "wordpress database name: %s\n" "$wpdbname" printf "wordpress database user: %s\n" "$wpdbuser" printf "wordpress database password: %s\n" "$wpdbpass"
#!/usr/bin/env bash usage() { printf "Usage: %s [options]\n" "$0" printf " -n hostname\n" printf " -m mariadb root password\n" printf " -p gitea database password\n" printf " -h Show help\n\n" printf "Example:\n" printf "%s -n git.something.xyz -m mariadbrootpwd -p giteadbpassword\n\n" "$0" } # Initialize variables hostname="" mariadbrootpwd="" giteadbpass="" # Parse CLI arguments with -m as second option while getopts ":n:m:p:h" option; do case "$option" in n) hostname="${OPTARG,,}" ;; # lowercase m) mariadbrootpwd="$OPTARG" ;; p) giteadbpass="$OPTARG" ;; h) usage; exit 0 ;; \?) printf "Invalid option: -%s\n" "$OPTARG"; usage; exit 1 ;; :) printf "Option -%s requires an argument.\n" "$OPTARG"; usage; exit 1 ;; esac done # === Required argument validation === validate_input() { local -a fields=( "hostname:hostname" "mariadb root password:mariadbrootpwd" "gitea database password:giteadbpass" ) local missing=false for field in "${fields[@]}"; do local label="${field%%:*}" local varname="${field##*:}" local value="${!varname}" if [[ -z "$value" ]]; then printf "Error! %s is empty.\n" "$label" missing=true fi done if [[ $missing == true ]]; then printf "\n" usage exit 1 fi } validate_input # Print all variables printf "\nhostname: %s\n" "$hostname" printf "mariadb root password: %s\n" "$mariadbrootpwd" printf "gitea database password: %s\n" "$giteadbpass"
First create a helper function that can parse variables and flags as well as dynamic usage.
#!/usr/bin/env bash # ======================== # Print usage dynamically # ======================== usage() { printf "Usage: %s [options]\n" "$0" 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" # Example command printf "Example:\n" example="$0" for flag in "${flags[@]}"; do short="${flag%%:*}" sample="${flag##*:}" example+=" -$short $sample" done printf "%s\n\n" "$example" } # ======================== # Validate required flags # ======================== validate_input() { local missing=false for flag in "${flags[@]}"; do short="${flag%%:*}" rest="${flag#*:}" var="${rest%%:*}" rest="${rest#*:}" desc="${rest%%:*}" rest="${rest#*:}" sample="${rest%%:*}" required="${rest##*:}" if [[ "$required" == "true" && -z "${!var}" ]]; then printf "Error! %s is empty.\n" "$desc" missing=true fi done if [[ $missing == true ]]; then printf "\n" usage exit 1 fi }
Then call it like this in your bash script.
#!/usr/bin/env bash # ======================== # Flags array (required only) # ======================== declare -a flags=( "n:hostname:hostname:wp.example.com:true" "m:mariadbrootpwd:MariaDB root password:rootpwd:true" ) # ======================== # Source helpers # ======================== source "$(dirname "$0")/helpers.sh" # ======================== # Parse CLI arguments # ======================== optstring="h" for flag in "${flags[@]}"; do short="${flag%%:*}" optstring+="$short:" done while getopts "$optstring" option; do case "$option" in h) usage; exit 0 ;; *) for flag in "${flags[@]}"; do short="${flag%%:*}" rest="${flag#*:}" var="${rest%%:*}" if [[ "$option" == "$short" ]]; then declare "$var"="$OPTARG" fi done ;; \?) printf "Invalid option: -%s\n" "$OPTARG"; usage; exit 1 ;; :) printf "Option -%s requires an argument.\n" "$OPTARG"; usage; exit 1 ;; esac done # ======================== # Validate required flags # ======================== validate_input # ======================== # Print parsed variables # ======================== echo "Hostname: $hostname" echo "MariaDB root password: $mariadbrootpwd"