#!/bin/bash #################################################################################################### #### author: SlickStack ############################################################################ #### link: https://slickstack.io ################################################################### #### mirror: http://mirrors.slickstack.io/bash/ss-functions.txt #################################### #### path: /var/www/ss-functions ################################################################### #### destination: n/a (not a boilerplate) ########################################################## #### purpose: Required functions and variables used by ss core cron jobs and bash scripts ########## #### module version: Ubuntu 20.04 LTS ############################################################## #### sourced by: ss core cron jobs, ss core bash scripts ########################################### #### bash aliases: n/a ############################################################################# #################################################################################################### ## SS-FUNCTIONS MUST BE INCLUDED IN ALL SS SCRIPTS IN ORDER FOR THEM TO WORK PROPERLY ## ## IT NOT ONLY SPECIFIES COMMAND FLAGS (OPTIONS) BUT ALSO PRETTIFIES THE SHELL ## #################################################################################################### #### SS-Functions: Critical Bash Variables ######################################################### #################################################################################################### ## SNIPPET: ss-functions, ss-install ## these are kind of like enviroment variables that are used within various ss scripts ## ## we keep these variables together for easy reference and better organization ## ## ss variables ## SS_CPU_CORES=`grep -c ^processor /proc/cpuinfo 2>/dev/null || sysctl -n hw.ncpu || echo "$NUMBER_OF_PROCESSORS"` SS_DISK_FREE=`df -kh --output=avail "$PWD" | tail -n1` SS_DISK_TOTAL=`df | grep '^/dev/' | awk '{s+=$2} END {print s/1048576}'` SS_EOF="SS_EOF" SS_IPV4_ADDRESS=`ip addr show $SS_NETWORK_INTERFACE | grep "inet " | awk '{ print $2;exit }' | cut -d/ -f1` SS_IPV6_ADDRESS=`ip addr show $SS_NETWORK_INTERFACE | grep "inet6 " | awk '{ print $2;exit }' | cut -d/ -f1` SS_LINUX_KERNEL=`uname -a` SS_NETWORK_INTERFACE=`ip route get 1.1.1.1 | head -n1 | awk '{print $5}'` SS_OS_VERSION=`lsb_release -ds` SLEEP_MESSAGE_BEGIN="1s" ## ss file mirror links ## MIRROR_SS_CHECK="http://mirrors.slickstack.io/bash/ss-check.txt" MIRROR_SS_CONFIG_SAMPLE="http://mirrors.slickstack.io/bash/ss-config-sample.txt" MIRROR_SS_WORKER="http://mirrors.slickstack.io/bash/ss-worker.txt" ## ss file paths (temp) ## TMP_SS_CONFIG="/tmp/ss-config" TMP_WORDPRESS_CONFIG_DEV="/tmp/wp-config-dev.php" TMP_WORDPRESS_CONFIG_PRODUCTION="/tmp/wp-config-production.php" TMP_WORDPRESS_CONFIG_STAGING="/tmp/wp-config-staging.php" ## ss file paths (final) ## PATH_SS_CHECK="/var/www/ss-check" PATH_SS_WORKER="/var/www/ss-worker" ## old stuff ## UBUNTU_VERSION=`lsb_release -rs` NETWORK_INTERFACE=`ip route get 1.1.1.1 | head -n1 | awk '{print $5}'` IPV4_ADDRESS=`ip addr show $NETWORK_INTERFACE | grep "inet " | awk '{ print $2;exit }' | cut -d/ -f1` IPV6_ADDRESS=`ip addr show $NETWORK_INTERFACE | grep "inet6 " | awk '{ print $2;exit }' | cut -d/ -f1` SS_HOSTNAME=`/bin/hostname -f` ## remove -f in future SS_HOSTNAME_ALL=`/bin/hostname -A` SS_HOSTNAME_FQDN=`/bin/hostname -f` SS_TLD=`hostname -d` # SS_MYSQL_VERSION=`mysqld --version` # SS_MYSQL_SIZE=`mysql --execute="SELECT table_schema AS "wordpress", SUM(data_length + index_length) / 1024 / 1024 / 1024 AS "Size (GB)" FROM information_schema.TABLES GROUP BY table_schema";` # SS_PHP_VERSION=`php -v | grep ^PHP | cut -d' ' -f2` # SS_PHP_EXTENSIONS=`php -r "print_r(implode(', ', get_loaded_extensions()));"` #################################################################################################### #### SS-Functions: Critical Bash Functions ######################################################### #################################################################################################### ## SNIPPET: ss-functions, ss-install ## these functions are sourced into ss core cron jobs and bash scripts for consistency ## ## this also speeds up processing time by avoiding repeat inline bash commands ## ## add-apt-repository alias flags ## function add-apt-repository { export DEBIAN_FRONTEND=noninteractive export DEBIAN_PRIORITY=critical export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' command /usr/bin/add-apt-repository --yes "$@" } ## apt alias flags ## function apt { export DEBIAN_FRONTEND=noninteractive export DEBIAN_PRIORITY=critical export PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' command /usr/bin/apt --yes --quiet --option Dpkg::Options::=--force-confold --option Dpkg::Options::=--force-confdef "$@" } ## cp alias flags ## function cp { command cp -R -f -d --no-preserve=mode,ownership "$@" } ## ln alias flags ## function ln { command ln -s -f "$@" } ## mkdir alias flags ## function mkdir { command mkdir -p "$@" } ## mysql alias flags (8.0) ## function mysql { command mysql --user=root --host=localhost --protocol=socket --port=3306 --force "$@" # mysql --user=root --host="$DB_HOST" --protocol=tcp --port=3306 --flush-privileges --force "$DB_NAME" # --max_allowed_packet=1GB ... add this after test } ## mysql_5.7 alias flags ## function mysql_5.7 { command mysql --user=root --password=$DB_PASSWORD_ROOT --host=127.0.0.1 --protocol=tcp --port=3306 --force "$@" } ## mysqldump alias flags ## # function mysqldump { # /usr/bin/mysqldump # command mysqldump --user=$DB_USER --password=$DB_PASSWORD --host=$DB_HOST --protocol=tcp --port=3306 --no-tablespaces --single-transaction --skip-lock-tables --dump-date --force "$@" # } ## mysqldump alias flags ## function mysqldump { # /usr/bin/mysqldump command mysqldump --user=$DB_USER --password=$DB_PASSWORD --host=$DB_HOST --protocol=tcp --port=3306 --no-tablespaces --single-transaction --skip-lock-tables --dump-date --force "$@" } ## rm alias flags ## function rm { command rm -R -f "$@" } ## rsync alias flags ## function rsync { command rsync -aI --ignore-errors "$@" } ## touch alias flags ## function touch { command touch -am "$@" } ## unzip alias flags ## function unzip { command unzip -o "$@" } ## wget alias flags ## function wget { command wget --quiet --no-check-certificate --inet4-only --no-cache --no-cookies --tries=3 --timeout=15 "$@" } #################################################################################################### #### SS-Functions: Pretty Colors + Styles For The Shell ############################################ #################################################################################################### ## SNIPPET: ss-functions, ss-install ## these color variables help maintain cleaner and prettier Bash scripts in SlickStack ## ## we substitute e.g. ${PINK} within any echo line then use ${RESET} to revert ## ## pretty colors old ## DARKRED='\033[0;31m' LIGHTRED='\033[1;31m' DARKGREEN='\033[0;32m' LIGHTGREEN='\033[1;32m' DARKCYAN='\033[0;36m' LIGHTCYAN='\033[1;36m' DARKBLUE='\033[0;34m' LIGHTBLUE='\033[1;34m' DARKPURPLE='\033[0;35m' LIGHTPURPLE='\033[1;35m' ## new ## GREEN='\033[1;32m' NOCOLOR='\033[0m' ORANGE='\033[0;33m' PINK='\033[1;35m' PURPLE='\033[0;35m' YELLOW='\033[1;33m' WHITE='\033[1;37m' DARKGRAY='\033[1;30m' LIGHTGRAY='\033[0;37m' BGBLACK=$(tput setab 0) # black BGRED=$(tput setab 1) # red BGGREEN=$(tput setab 2) # green BGYELLOW=$(tput setab 3) # yellow BGBLUE=$(tput setab 4) # blue BGPINK=$(tput setab 5) # magenta (pink) BGCYAN=$(tput setab 6) # cyan BGWHITE=$(tput setab 7) # white ## pretty styles ## BOLD=$(tput bold) # bold (strong) DIM=$(tput dim) # dim (half-bright) NORMAL=$(tput sgr0) # reset all styles (normal) RESET=$(tput sgr0) # reset all styles (normal) BLINK=$(tput blink) # blink UNDERLINE=$(tput smul) # underline ENDUNDER=$(tput rmul) # end underline REVERSE=$(tput rev) # reverse STANDOUT=$(tput smso) # standout (highlight) ENDSTAND=$(tput rmso) # end standout #################################################################################################### #### SlickStack: External References Used To Improve This Script (Thanks, Interwebz) ############### #################################################################################################### ## Ref: https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/lsbrelease.html ## Ref: https://www.tecmint.com/8-pratical-examples-of-linux-touch-command/ ## Ref: https://stackoverflow.com/questions/6481005/how-to-obtain-the-number-of-cpus-cores-in-linux-from-the-command-line ## Ref: https://ostechnix.com/find-out-the-linux-distribution-name-version-and-kernel-details/ ## Ref: https://linuxize.com/post/how-to-check-mysql-version/ ## Ref: https://www.cyberciti.biz/faq/find-my-linux-machine-name/ ## Ref: https://unix.stackexchange.com/questions/471521/how-to-get-only-the-version-number-of-php ## Ref: https://www.geeksforgeeks.org/how-to-create-comma-separated-list-from-array-in-php/ ## Ref: https://stackoverflow.com/questions/478844/how-do-i-see-the-extensions-loaded-by-php ## Ref: https://linux.die.net/man/1/dnsdomainname ## Ref: https://serverfault.com/questions/739684/why-hostname-f-gives-me-subdomain-back-when-only-domain-is-in-etc-hostname ## Ref: https://serverfault.com/questions/545215/change-ps1-value-for-all-bash-users ## Ref: https://serverfault.com/questions/218629/most-effective-way-to-change-linux-command-prompt-for-all-users ## Ref: https://askubuntu.com/questions/984060/export-ps1-for-customizing-shell-prompt ## Ref: https://serverfault.com/questions/296970/bash-prompt-on-ubuntu-fqdn-h-same-as-hostname-h ## Ref: https://www.howtoforge.com/tutorial/linux-touch-command/ ## Ref: https://www.binarytides.com/linux-touch-command/ ## Ref: http://www.linfo.org/touch.html ## Ref: https://superuser.com/questions/351251/get-total-size-of-my-hard-drive-in-linux-using-the-command-line-without-root-p ## Ref: https://www.shellhacks.com/bash-colors/ ## Ref: https://misc.flogisoft.com/bash/tip_colors_and_formatting ## Ref: https://www.geeksforgeeks.org/hostname-command-in-linux-with-examples/ ## Ref: https://dev.mysql.com/doc/refman/8.0/en/packet-too-large.html ## Ref: https://superuser.com/questions/161110/why-doesnt-hostname-fqdn-work-on-my-ubuntu-computer ## Ref: https://techpiezo.com/linux/wget-force-ipv4-ipv6-connection/ ## Ref: https://unix.stackexchange.com/questions/168584/wget-is-unable-to-resolve-host-address-80-of-the-time ## Ref: https://github.community/t/cannot-reach-any-github-io-page-via-ipv6/10310 ## Ref: https://github.com/isaacs/github/issues/354 ## SS_EOF