mirror of
https://github.com/natelandau/shell-scripting-templates.git
synced 2025-11-08 13:13:47 -05:00
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
451 lines
13 KiB
Bash
451 lines
13 KiB
Bash
# Functions for manipulating arrays
|
|
|
|
_dedupeArray_() {
|
|
# DESC:
|
|
# Removes duplicate array elements
|
|
# ARGS:
|
|
# $1 (Required) - Input array
|
|
# OUTS:
|
|
# stdout: Prints de-duped elements
|
|
# USAGE:
|
|
# _removeDups_ "${array[@]}"
|
|
# NOTE:
|
|
# List order may not stay the same
|
|
# CREDIT:
|
|
# https://github.com/dylanaraps/pure-bash-bible
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
declare -A _tmpArray
|
|
declare -a _uniqueArray
|
|
local _i
|
|
for _i in "$@"; do
|
|
{ [[ -z ${_i} || ${_tmpArray[${_i}]:-} ]]; } && continue
|
|
_uniqueArray+=("${_i}") && _tmpArray[${_i}]=x
|
|
done
|
|
printf '%s\n' "${_uniqueArray[@]}"
|
|
}
|
|
|
|
_forEachDo_() {
|
|
# DESC:
|
|
# Iterates over elements and passes each to a function
|
|
# ARGS:
|
|
# $1 (Required) - Function name to pass each item to
|
|
# OUTS:
|
|
# 0 - Success
|
|
# Return code of called function
|
|
# stdout: Output of called function
|
|
# USAGE:
|
|
# printf "%s\n" "${arr1[@]}" | _forEachDo_ "test_func"
|
|
# _forEachDo_ "test_func" < <(printf "%s\n" "${arr1[@]}") #alternative approach
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
local _func="${1}"
|
|
local IFS=$'\n'
|
|
local _it
|
|
|
|
while read -r _it; do
|
|
if [[ ${_func} == *"$"* ]]; then
|
|
eval "${_func}"
|
|
else
|
|
[ ! "$(declare -f "${_func}")" ] && fatal "${FUNCNAME[0]} could not find function ${_func}"
|
|
eval "${_func}" "'${_it}'"
|
|
fi
|
|
declare -i _ret="$?"
|
|
|
|
if [[ ${_ret} -ne 0 ]]; then
|
|
return ${_ret}
|
|
fi
|
|
done
|
|
}
|
|
|
|
_forEachValidate_() {
|
|
# DESC:
|
|
# Iterates over elements and passes each to a function for validation. Iteration stops when the function returns 1.
|
|
# ARGS:
|
|
# $1 (Required) - Function name to pass each item to for validation. (Must return 0 on success)
|
|
# OUTS:
|
|
# 0 - Success
|
|
# 1 - Iteratee function fails
|
|
# USAGE:
|
|
# printf "%s\n" "${array[@]}" | _forEachValidate_ "_isAlpha_"
|
|
# _forEachValidate_ "_isAlpha_" < <(printf "%s\n" "${array[@]}")
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
local _func="${1}"
|
|
local IFS=$'\n'
|
|
local _it
|
|
|
|
while read -r _it; do
|
|
if [[ ${_func} == *"$"* ]]; then
|
|
eval "${_func}"
|
|
else
|
|
[ ! "$(declare -f "${_func}")" ] && fatal "${FUNCNAME[0]} could not find function ${_func}"
|
|
eval "${_func}" "'${_it}'"
|
|
fi
|
|
declare -i _ret="$?"
|
|
|
|
if [[ ${_ret} -ne 0 ]]; then
|
|
return 1
|
|
fi
|
|
done
|
|
}
|
|
|
|
_forEachFind_() {
|
|
# DESC:
|
|
# Iterates over elements, returning the first value that is validated by a function
|
|
# ARGS:
|
|
# $1 (Required) - Function name to pass each item to for validation. (Must return 0 on success)
|
|
# OUTS:
|
|
# 0 - If successful
|
|
# 1 - If iteratee function fails
|
|
# stdout: First value that is validated by the function
|
|
# USAGE:
|
|
# printf "%s\n" "${array[@]}" | _forEachFind_ "_isAlpha_"
|
|
# _forEachFind_ "_isAlpha_" < <(printf "%s\n" "${array[@]}")
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
declare _func="${1}"
|
|
declare IFS=$'\n'
|
|
while read -r _it; do
|
|
|
|
if [[ ${_func} == *"$"* ]]; then
|
|
eval "${_func}"
|
|
else
|
|
eval "${_func}" "'${_it}'"
|
|
fi
|
|
declare -i _ret="$?"
|
|
if [[ ${_ret} == 0 ]]; then
|
|
printf "%s" "${_it}"
|
|
return 0
|
|
fi
|
|
done
|
|
|
|
return 1
|
|
}
|
|
|
|
_forEachFilter_() {
|
|
# DESC:
|
|
# Iterates over elements, returning only those that are validated by a function
|
|
# ARGS:
|
|
# $1 (Required) - Function name to pass each item to for validation. (Must return 0 on success)
|
|
# OUTS:
|
|
# 0 - Success
|
|
# 1 - Failure
|
|
# stdout: Values matching the validation function
|
|
# USAGE:
|
|
# printf "%s\n" "${array[@]}" | _forEachFind_ "_isAlpha_"
|
|
# _forEachFilter_ "_isAlpha_" < <(printf "%s\n" "${array[@]}")
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
local _func="${1}"
|
|
local IFS=$'\n'
|
|
while read -r _it; do
|
|
if [[ ${_func} == *"$"* ]]; then
|
|
eval "${_func}"
|
|
else
|
|
eval "${_func}" "'${_it}'"
|
|
fi
|
|
declare -i _ret="$?"
|
|
if [[ ${_ret} == 0 ]]; then
|
|
printf "%s\n" "${_it}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
_forEachReject_() {
|
|
# DESC:
|
|
# The opposite of _forEachFilter_. Iterates over elements, returning only those that are not validated by a function
|
|
# ARGS:
|
|
# $1 (Required) - Function name to pass each item to for validation. (Must return 0 on success, 1 on failure)
|
|
# OUTS:
|
|
# 0 - Success
|
|
# stdout: Values NOT matching the validation function
|
|
# USAGE:
|
|
# printf "%s\n" "${array[@]}" | _forEachReject_ "_isAlpha_"
|
|
# _forEachReject_ "_isAlpha_" < <(printf "%s\n" "${array[@]}")
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
local _func="${1}"
|
|
local IFS=$'\n'
|
|
while read -r _it; do
|
|
if [[ ${_func} == *"$"* ]]; then
|
|
eval "${_func}"
|
|
else
|
|
eval "${_func}" "'${_it}'"
|
|
fi
|
|
declare -i _ret=$?
|
|
if [[ ${_ret} -ne 0 ]]; then
|
|
echo "${_it}"
|
|
fi
|
|
done
|
|
}
|
|
|
|
_forEachSome_() {
|
|
# DESC:
|
|
# Iterates over elements, returning true if any of the elements validate as true from the function.
|
|
# ARGS:
|
|
# $1 (Required) - Function name to pass each item to for validation. (Must return 0 on success, 1 on failure)
|
|
# OUTS:
|
|
# 0 If match successful
|
|
# 1 If no match found
|
|
# USAGE:
|
|
# printf "%s\n" "${array[@]}" | _forEachSome_ "_isAlpha_"
|
|
# _forEachSome_ "_isAlpha_" < <(printf "%s\n" "${array[@]}")
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
local _func="${1}"
|
|
local _IFS=$'\n'
|
|
while read -r _it; do
|
|
|
|
if [[ ${_func} == *"$"* ]]; then
|
|
eval "${_func}"
|
|
else
|
|
eval "${_func}" "'${_it}'"
|
|
fi
|
|
|
|
declare -i _ret=$?
|
|
if [[ ${_ret} -eq 0 ]]; then
|
|
return 0
|
|
fi
|
|
done
|
|
|
|
return 1
|
|
}
|
|
|
|
_inArray_() {
|
|
# DESC:
|
|
# Determine if a value is in an array. Default is case sensitive.
|
|
# Pass -i flag to ignore case.
|
|
# ARGS:
|
|
# $1 (Required) - Value to search for
|
|
# $2 (Required) - Array written as ${ARRAY[@]}
|
|
# OUTS:
|
|
# 0 if true
|
|
# 1 if untrue
|
|
# USAGE:
|
|
# if _inArray_ "VALUE" "${ARRAY[@]}"; then ...
|
|
# if _inArray_ -i "VALUE" "${ARRAY[@]}"; then ...
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# -lt 2 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
local opt
|
|
local _case_insensitive=false
|
|
local OPTIND=1
|
|
while getopts ":iI" opt; do
|
|
case ${opt} in
|
|
i | I) _case_insensitive=true ;;
|
|
*) fatal "Unrecognized option '${1}' passed to ${FUNCNAME[0]}. Exiting." ;;
|
|
esac
|
|
done
|
|
shift $((OPTIND - 1))
|
|
|
|
local _array_item
|
|
local _value="${1}"
|
|
shift
|
|
for _array_item in "$@"; do
|
|
if [ ${_case_insensitive} = true ]; then
|
|
_value="$(echo "${_value}" | tr '[:upper:]' '[:lower:]')"
|
|
_array_item="$(echo "${_array_item}" | tr '[:upper:]' '[:lower:]')"
|
|
fi
|
|
[[ ${_array_item} == "${_value}" ]] && return 0
|
|
done
|
|
return 1
|
|
}
|
|
|
|
_isEmptyArray_() {
|
|
# DESC:
|
|
# Checks if an array is empty
|
|
# ARGS:
|
|
# $1 (Required) - Input array
|
|
# OUTS:
|
|
# 0 if empty
|
|
# 1 if not empty
|
|
# USAGE:
|
|
# _isEmptyArray_ "${array[@]}"
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
declare -a _array=("$@")
|
|
if [ ${#_array[@]} -eq 0 ]; then
|
|
return 0
|
|
else
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
_joinArray_() {
|
|
# DESC:
|
|
# Joins items together with a user specified separator
|
|
# ARGS:
|
|
# $1 (Required) - Separator
|
|
# $@ (Required) - Array or space separated items to be joined
|
|
# OUTS:
|
|
# stdout: Prints joined terms
|
|
# USAGE:
|
|
# _join_ , a "b c" d #a,b c,d
|
|
# _join_ / var local tmp #var/local/tmp
|
|
# _join_ , "${foo[@]}" #a,b,c
|
|
# CREDIT:
|
|
# http://stackoverflow.com/questions/1527049/bash-join-elements-of-an-array
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# -lt 2 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
local _delimiter="${1}"
|
|
shift
|
|
printf "%s" "${1}"
|
|
shift
|
|
printf "%s" "${@/#/${_delimiter}}"
|
|
}
|
|
|
|
_mergeArrays_() {
|
|
# DESC:
|
|
# Merges two arrays together
|
|
# ARGS:
|
|
# $1 (Required) - Array 1
|
|
# $2 (Required) - Array 2
|
|
# OUTS:
|
|
# stdout: Prints result
|
|
# USAGE:
|
|
# newarray=($(_mergeArrays_ "array1[@]" "array2[@]"))
|
|
# NOTE:
|
|
# Note that the arrays must be passed in as strings
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# -ne 2 ]] && fatal 'Missing required argument to _mergeArrays_'
|
|
declare -a _arr1=("${!1}")
|
|
declare -a _arr2=("${!2}")
|
|
declare _outputArray=("${_arr1[@]}" "${_arr2[@]}")
|
|
printf "%s\n" "${_outputArray[@]}"
|
|
}
|
|
|
|
_reverseSortArray_() {
|
|
# DESC:
|
|
# Sorts an array from lowest to highest (z-a9-0)
|
|
# ARGS:
|
|
# $1 (Required) - Input array
|
|
# OUTS:
|
|
# stdout: Prints result
|
|
# USAGE:
|
|
# _reverseSortArray_ "${array[@]}"
|
|
# NOTE:
|
|
# input=("c" "b" "4" "1" "2" "3" "a")
|
|
# _reverseSortArray_ "${input[@]}"
|
|
# c b a 4 3 2 1
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
declare -a _array=("$@")
|
|
declare -a _sortedArray
|
|
mapfile -t _sortedArray < <(printf '%s\n' "${_array[@]}" | sort -r)
|
|
printf "%s\n" "${_sortedArray[@]}"
|
|
}
|
|
|
|
_randomArrayElement_() {
|
|
# DESC:
|
|
# Selects a random item from an array
|
|
# ARGS:
|
|
# $1 (Required) - Input array
|
|
# OUTS:
|
|
# stdout: Prints one random element
|
|
# USAGE:
|
|
# _randomArrayElement_ "${array[@]}"
|
|
# CREDIT:
|
|
# https://github.com/dylanaraps/pure-bash-bible
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
declare -a _array
|
|
local _array=("$@")
|
|
printf '%s\n' "${_array[RANDOM % $#]}"
|
|
}
|
|
|
|
_setDiff_() {
|
|
# DESC:
|
|
# Return items that exist in ARRAY1 that are do not exist in ARRAY2
|
|
# ARGS:
|
|
# $1 (Required) - Array 1 (in format ARRAY[@])
|
|
# $2 (Required) - Array 2 (in format ARRAY[@])
|
|
# OUTS:
|
|
# 0 if unique elements found
|
|
# 1 if arrays are the same
|
|
# stdout: Prints unique elements
|
|
# USAGE:
|
|
# _setDiff_ "array1[@]" "array2[@]"
|
|
# mapfile -t NEW_ARRAY < <(_setDiff_ "array1[@]" "array2[@]")
|
|
# NOTE:
|
|
# Note that the arrays must be passed in as strings
|
|
# CREDIT:
|
|
# http://stackoverflow.com/a/1617303/142339
|
|
|
|
[[ $# -lt 2 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
|
|
local _skip
|
|
local _a
|
|
local _b
|
|
declare -a _setdiffA=("${!1}")
|
|
declare -a _setdiffB=("${!2}")
|
|
declare -a _setdiffC=()
|
|
|
|
for _a in "${_setdiffA[@]}"; do
|
|
_skip=
|
|
for _b in "${_setdiffB[@]}"; do
|
|
if [[ ${_a} == "${_b}" ]]; then
|
|
_skip=1
|
|
break
|
|
fi
|
|
done
|
|
[[ "${_skip}" ]] || _setdiffC=("${_setdiffC[@]}" "${_a}")
|
|
done
|
|
|
|
if [[ ${#_setdiffC[@]} == 0 ]]; then
|
|
return 1
|
|
else
|
|
printf "%s\n" "${_setdiffC[@]}"
|
|
fi
|
|
}
|
|
|
|
_sortArray_() {
|
|
# DESC:
|
|
# Sorts an array from lowest to highest (0-9 a-z)
|
|
# ARGS:
|
|
# $1 (Required) - Input array
|
|
# OUTS:
|
|
# stdout: Prints result
|
|
# USAGE:
|
|
# _sortArray_ "${array[@]}"
|
|
# NOTE:
|
|
# input=("c" "b" "4" "1" "2" "3" "a")
|
|
# _sortArray_ "${input[@]}"
|
|
# 1 2 3 4 a b c
|
|
# CREDIT:
|
|
# https://github.com/labbots/bash-utility
|
|
|
|
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
|
|
declare -a _array=("$@")
|
|
declare -a _sortedArray
|
|
mapfile -t _sortedArray < <(printf '%s\n' "${_array[@]}" | sort)
|
|
printf "%s\n" "${_sortedArray[@]}"
|
|
}
|