Files
shell-scripting-templates/utilities/checks.bash
Nathaniel Landau 8bb89541e8 Squashed commit of the following:
commit 61bf734812cb62ba6e0ec224bc15f7928705a8a2
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Oct 21 15:44:21 2021 -0400

    Major overhaul continued

     - rename templates
     - add checks utilities
     - add new array utilities
     - rename files
     - add assorted utilities
     - improve documentation

commit 546178fff3b526f492eb0eeffc63f79537e75de3
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Wed Oct 20 16:31:14 2021 -0400

    Update conventions

commit f6d0642f85518efda9c5d8472b99d1c14163e381
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Wed Oct 20 09:47:09 2021 -0400

    minor formatting changes

commit 2217612b55e3f9faf803a2d0c937ea2261206505
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 19 17:59:09 2021 -0400

    add new functions

commit 347ba7aa738dcd6a5ad9d70886b38da3a17dc89e
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 19 12:06:44 2021 -0400

    major overhaul

    - Add standaloneTemplate.sh
    - Rework README
    - Refactor inline documentation
    - Enforce coding standards
    - Remove CSV utilities
    - Add new array utilities
    - add _useGNUutils_
    - more ...

commit cd8e0d49aef25eeaf6b3e71a3c9e1f29ab9b06f5
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sun Oct 17 09:56:08 2021 -0400

    Add debug functions

commit f7c5c0a3d19815dcc6ba80b5f5a2ebb77ef88b07
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sat Oct 16 21:10:01 2021 -0400

    add new array functions

    _joinArray_, _isEmptyArray_, _sortArray_, _reverseSortArray_, and _mergearrays_

commit d8bc3d8cabdbcee3c479f97b43a45bdfe3bdafe0
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Oct 15 17:27:12 2021 -0400

    add _columnize_

commit 2fd2ae9435f476bc3968c3eb0d793db4bf1d9eaf
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Oct 11 22:17:45 2021 -0400

    _progressBar_: Fix unbound variable

commit e8933d15fc955a1acc665e9a081f131e681855d5
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Sun Oct 10 11:50:42 2021 -0400

    _alert_: header now underlined

commit c9ce894361dec7d3513c038794a155519baf26bc
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:49:42 2021 -0400

    _alert_: line numbers to gray

commit 4aaddd336ce613f629a7e6a62ef3b27ffc24d22d
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Oct 8 15:05:20 2021 -0400

    _usage_ to stdout

commit e2372fc3122ec1f20acc27f04d29b3785f014e25
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:38:26 2021 -0400

    _setPATH_: remove unneeded logic

commit e60c75b6c954ac4bd146e2758252168027b9a43d
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Tue Oct 5 09:25:38 2021 -0400

    _findSource_: bugfix

commit 0e84912e1ccd7203e5beff9f8737f8374f4aa5d8
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Sep 30 16:29:25 2021 -0400

    add requirements to documentation

commit 2c24843e3ada591e1868a94416e40b5ac0aa4994
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Thu Sep 30 15:34:10 2021 -0400

    _uniqueFilename_: improve extension handling

commit 08bc2dfdcc8632efee9179e9c960a574fc17cf0c
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 27 15:13:53 2021 -0400

    improve hooks script

commit 641918f1559d3b3aa38a9bbdf418938b2b81c176
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Sep 24 08:16:52 2021 -0400

    _inArry_: case insensitivity

commit eae10f170680540fdb4a1222add7e54f8785ea63
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:31:44 2021 -0400

    clean up alerting

commit 700acd56f57fd57db84ef0e232ef41cdd7aee43c
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:22:11 2021 -0400

    refactor _execute_

commit d893f86900a9fed9d91a0c9cc06c13b6b34d9926
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Mon Sep 20 18:19:18 2021 -0400

    'fatal' replaces 'die'

commit 3326857bf127bef36cd9982246aa5b826d796d0a
Author: Nathaniel Landau <nate@natelandau.com>
Date:   Fri Sep 17 08:29:50 2021 -0400

    _execute_: ensure quiet and verbose work together
2021-10-21 16:03:27 -04:00

353 lines
9.3 KiB
Bash

# Functions for validating common use-cases
_binaryExists_() {
# DESC:
# Check if a binary exists in the search PATH
# ARGS:
# $1 (Required) - Name of the binary to check for existence
# OUTS:
# 0 if true
# 1 if false
# USAGE:
# (_binaryExists_ ffmpeg ) && [SUCCESS] || [FAILURE]
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
if ! command -v "$1" >/dev/null 2>&1; then
debug "Did not find dependency: '${1}'"
return 1
fi
return 0
}
_functionExists_() {
# DESC:
# Tests if a function exists in the current scope
# ARGS:
# $1 (Required) - Function name
# OUTS:
# 0 if function exists
# 1 if function does not exist
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _testFunction
_testFunction="${1}"
if declare -f "${_testFunction}" &>/dev/null 2>&1; then
return 0
else
return 1
fi
}
_isAlpha_() {
# DESC:
# Validate that a given input is entirely alphabetic characters
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Input is only alphabetic characters
# 1 - Input contains non-alphabetic characters
# USAGE:
# _isAlpha_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alpha:]]+$'
if [[ ${1} =~ ${_re} ]]; then
return 0
fi
return 1
}
_isAlphaNum_() {
# DESC:
# Validate that a given input is entirely alpha-numeric characters
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Input is only alpha-numeric characters
# 1 - Input contains alpha-numeric characters
# USAGE:
# _isAlphaNum_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alnum:]]+$'
if [[ ${1} =~ ${_re} ]]; then
return 0
fi
return 1
}
_isAlphaDash_() {
# DESC:
# Validate that a given input contains only alpha-numeric characters, as well as dashes and underscores.
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Input is only alpha-numeric or dash or underscore characters
# 1 - Input is not only alpha-numeric or dash or underscore characters
# USAGE:
# _isAlphaDash_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:alnum:]_-]+$'
if [[ ${1} =~ ${_re} ]]; then
return 0
fi
return 1
}
_isEmail_() {
# DESC:
# Validates that input is a valid email address
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Is valid email
# 1 - Is not valid email
# USAGE:
# _isEmail_ "somename+test@gmail.com"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _emailRegex
_emailRegex="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
[[ ${1} =~ ${_emailRegex} ]] && return 0 || return 1
}
_isInternetAvailable_() {
# DESC:
# Check if internet connection is available
# ARGS:
# None
# OUTS:
# 0 - Success: Internet connection is available
# 1 - Failure: Internet connection is not available
# stdout:
# USAGE:
# _isInternetAvailable_
# NOTES:
#
local _checkInternet
if [[ -t 1 || -z ${TERM} ]]; then
_checkInternet="$(sh -ic 'exec 3>&1 2>/dev/null; { curl --compressed -Is google.com 1>&3; kill 0; } | { sleep 10; kill 0; }' || :)"
else
_checkInternet="$(curl --compressed -Is google.com -m 10)"
fi
if [[ -z ${_checkInternet:-} ]]; then
return 1
fi
}
_isIPv4_() {
# DESC:
# Validates that input is a valid IP version 4 address
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Is valid IPv4 address
# 1 - Is not valid IPv4 address
# USAGE:
# _isIPv4_ "192.168.1.1"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _ip="${1}"
local IFS=.
# shellcheck disable=SC2206
declare -a _a=(${_ip})
[[ ${_ip} =~ ^[0-9]+(\.[0-9]+){3}$ ]] || return 1
# Test values of quads
local _quad
for _quad in {0..3}; do
[[ ${_a[${_quad}]} -gt 255 ]] && return 1
done
return 0
}
_isFile_() {
# DESC:
# Validate that a given input points to a valid file
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Input is a valid file
# 1 - Input is not a valid file
# USAGE:
# _varIsFile_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
[[ -f ${1} ]] && return 0 || return 1
}
_isDir_() {
# DESC:
# Validate that a given input points to a valid directory
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Input is a directory
# 1 - Input is not a directory
# USAGE:
# _varIsDir_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
[[ -d ${1} ]] && return 0 || return 1
}
_isNum_() {
# DESC:
# Validate that a given input is entirely numeric characters
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Input is only numeric characters
# 1 - Input contains numeric characters
# USAGE:
# _isNum_ "${var}"
# NOTES:
#
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _re='^[[:digit:]]+$'
if [[ ${1} =~ ${_re} ]]; then
return 0
fi
return 1
}
_isTerminal_() {
# DESC:
# Check is script is run in an interactive terminal
# ARGS:
# None
# OUTS:
# 0 - Script is run in a terminal
# 1 - Script is not run in a terminal
# USAGE:
# _isTerminal_
[[ -t 1 || -z ${TERM} ]] && return 0 || return 1
}
_rootAvailable_() {
# DESC:
# Validate we have superuser access as root (via sudo if requested)
# ARGS:
# $1 (optional): Set to any value to not attempt root access via sudo
# OUTS:
# 0 if true
# 1 if false
# CREDIT:
# https://github.com/ralish/bash-script-template
local _superuser
local _testEUID
if [[ ${EUID} -eq 0 ]]; then
_superuser=true
elif [[ -z ${1:-} ]]; then
if command -v sudo >/dev/null 2>&1; then
debug 'Sudo: Updating cached credentials ...'
if ! sudo -v; then
warning "Sudo: Couldn't acquire credentials ..."
else
_testEUID="$(sudo -H -- "$BASH" -c 'printf "%s" "$EUID"')"
if [[ ${_testEUID} -eq 0 ]]; then
_superuser=true
fi
fi
fi
fi
if [[ -z ${superuser:-} ]]; then
debug 'Unable to acquire superuser credentials.'
return 1
fi
debug 'Successfully acquired superuser credentials.'
return 0
}
_varIsTrue_() {
# DESC:
# Check if a given variable is true
# ARGS:
# $1 (required): Variable to check
# OUTS:
# 0 - Variable is true
# 1 - Variable is false
# USAGE
# _varIsTrue_ "${var}"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
[[ ${1} == true || ${1} -eq 0 ]] && return 0 || return 1
}
_varIsFalse_() {
# DESC:
# Check if a given variable is false
# ARGS:
# $1 (required): Variable to check
# OUTS:
# 0 - Variable is false
# 1 - Variable is true
# USAGE
# _varIsFalse_ "${var}"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
[[ ${1} == false || ${1} -eq 1 ]] && return 0 || return 1
}
_varIsEmpty_() {
# DESC:
# Check if given variable is empty or null.
# ARGS:
# $1 (required): Variable to check
# OUTS:
# 0 - Variable is empty or null
# 1 - Variable is not empty or null
# USAGE
# _varIsEmpty_ "${var}"
[[ -z ${1} || ${1} == "null" ]] && return 0 || return 1
}
_isIPv6_() {
# DESC:
# Validates that input is a valid IP version 46address
# ARGS:
# $1 (required): Input to check
# OUTS:
# 0 - Is valid IPv6 address
# 1 - Is not valid IPv6 address
# USAGE:
# _isIPv6_ "2001:db8:85a3:8d3:1319:8a2e:370:7348"
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _ip="${1}"
local _re="^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|\
([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|\
([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|\
([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|\
:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|\
::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|\
(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|\
(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$"
[[ ${_ip} =~ ${_re} ]] && return 0 || return 1
}