ARCHIVED POST - This is old, links might not work

While far from complete, this bash script was created for a variety of reasons. The idea developed when localwp refused to install on my system due to a dependency issue libncurses5.

After briefly searching github nothing really seemed to pop out as a functional alternative. Given I just recently had my knee operated on, I figured it would be a good opportunity to learn more about bash scripting while creating something functional.

I’d never actually scripted with getopts either. And to make things even harder, I figured I’d try to write it POSIX compliant..

I’ll be updating the project at the github repository https://github.com/DTRHnet/wp-auto, so if you are seeing this there is a good chance it’s been updated since. Check it out.

  • This script has yet to incorporate -v, -l log.file, -c flags. That means adding verbosity, writing to a log file or disabling colour are non functional

  • This script does not yet create sql databases. In fact, due to my distro, I’ve disabled the MySQL installation and have yet to write SQL database configurations

I’ll get around to working more on this when I can walk across the room to my actual computer.

wp-auto.sh#!/usr/bin/env bash

# ######################################
#  ------------------------------------
#    ██████╗ ████████╗██████╗ ██╗  ██╗     Title      : Wordpress Auto Installer
#    ██╔══██╗╚══██╔══╝██╔══██╗██║  ██║     Filename   : dtrh-wpauto.sh
#    ██║  ██║   ██║   ██████╔╝███████║     Version    : 0.0.1
#    ██║  ██║   ██║   ██╔══██╗██╔══██║     Date       : 09.09.23
#    ██████╔╝   ██║   ██║  ██║██║  ██║
#    ╚═════╝    ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝     Additional : README.md
#            https://dtrh.net
#  ------------------------------------
# ######################################

# Set shell options
set -o posix
set -o errexit
set -o errtrace
set -o nounset
set -o pipefail

# Clear terminal
clear

# Initialize magic variables
__dir="$(cd "$(dirname "${BASH_SOURCE}")" && pwd)"
__file="${__dir}/$(basename "${BASH_SOURCE}")"
__base="$(basename "${__file}" .sh)"
__me="${BASH_SOURCE}"

# Initialize global variables
__HELP__=0
__NO_BANNER__=0
__COLOR__=1
__VERBOSE__=0
__INTERACTIVE__=0
__UNEXPECTED__=0
__LOG_FILE__="/tmp/wpauto.log"
__bad_opt__=""
__INSTALL_DIR__=""
__INSTALL_USR__=""
declare -a __ARGS__


# Define colour variables
NC="\033[0m"
BNC="\033[1m"
BLK="\033[0;30m"
BBLk="\033[1;30m"
RED="\033[0;31m"
BRED="\033[1;31m"
GRN="\033[0;32m"
BGRN="\033[1;32m"
YLW="\033[0;33m"
BYLW="\033[1;33m"
BLU="\033[0;34m"
BBLU="\033[1;34m"
PPL="\033[0;35m"
BPPL="\033[1;35m"
CYN="\033[0;36m"
BCYN="\033[1;36m"

# Root check
if [ ${EUID} -ne 0 ]; then
  echo -n -e "${BYLW}[${__base^^}] ${BRED}ERROR:\n${NC}Run script with ${BNC}root ${NC}privileges!"
  exit
fi

# Function definitions -----------------------------------------------------------------------------

# Function : Banner - Display DTRH banner
__banner() {
  if [ ${__NO_BANNER__} = 0 ]; then
    cat <<EOF 
# ######################################
#  ------------------------------------
#    ██████╗ ████████╗██████╗ ██╗  ██╗     Title      : Wordpress Auto Installer
#    ██╔══██╗╚══██╔══╝██╔══██╗██║  ██║     Filename   : dtrh-wpauto.sh
#    ██║  ██║   ██║   ██████╔╝███████║     Version    : 0.0.1
#    ██║  ██║   ██║   ██╔══██╗██╔══██║     Date       : 09.09.23
#    ██████╔╝   ██║   ██║  ██║██║  ██║
#    ╚═════╝    ╚═╝   ╚═╝  ╚═╝╚═╝  ╚═╝     Additional : README.md
#            https://dtrh.net
#  ------------------------------------
# ######################################


EOF
  fi
}

# Function : Usage - Display simple usage information
__usage() {
  __banner
cat <<EOF
Usage: ${__me} [OPTION]... [FLAG]...  [USER]
OR     ${__me} -i [OPTION]... [FLAG]...

       
EOF
}

# Function : Help - Display detailed usage information 
__help() {
  __banner
  echo -n -e "${BYLW}[${__base^^}] ${BBLU}HELP:${NC}\n"
  cat <<EOF

Usage: ${__me} [OPTION]... [FLAG]... [USER]
OR     ${__me} -i [OPTION]... [FLAG]...  


Options:
  -l [FILE]      Create logfile. Write to [FILE]
  
Flags:
  -h             Display this text
  -b             Disable banner    [Default: Disabled]
  -c             Disable color     [Default: Disabled]
  -i             Interactive Mode  [Default: Disabled]
  -v             Verbose output    [Default: Disabled]
EOF
  exit 1
}

# getopts loop : Interpret user input & sort
while getopts :bhl:icv opt; do
  case "${opt}" in
    b) __NO_BANNER__=1;;
    h) __HELP__=1;; 
    l) __LOG_FILE__="${OPTARG}";;
    :) __UNEXPECTED__=2;;
    i) __INTERACTIVE__=1;;
    c) __NO_COLOR__=1;;
    v) __VERBOSE__=1;;
    \?) __UNEXPECTED__=1; __bad_opt__=${opt};;
  esac
done

# Shift through options index one by one
shift $((${OPTIND} - 1))

# TODO: This checks if nothing but the script is run (no user input otherwise).
#       Because the array to hold arguments is of an unknown size, initializing it
#       properly is problematic for the argument count. Fix. Likely use 'unset' 
#       before counting args, and remove this snippet.
if [ $ -gt 0; do
  __ARGS__[$]}
  shift
done

# Begin calls after sorting or no-banner/no-color etc may not be processed properly
# eg. ${__me} -h -b   will call help first before switching the no-banner flag
#                     resulting in banner displaying. This is why functions are not
#                     called directly in getopts loop.
if [ ${__HELP__} = 1 ]; then 
  __help
fi

# Options are parsed out, expectation is two arguments remain
# Throw appropriate errors if array is not expected size
if [ ! $

Recents Post

Share

08

Vibe Coding with LLM Agents Friday, 6PM
PUBLIC SERVICE ANNOUNCEMENTI’m not a (total) doomster. But I’m very much well informed. And if I ...

27

Tuesday, 1AM
Context Wrangler 🚦Automated context-length hygiene • Multi-model AI prompt generator • Git-to-Ro...

Powered by Hexo