Files
shell-scripting-templates/utilities/debug.bash
2021-11-05 20:38:47 -04:00

67 lines
1.9 KiB
Bash

# Functions to aid in debugging bash scripts
_pauseScript_() {
# DESC:
# Pause a script at any point and continue after user input
# ARGS:
# $1 (Optional) - String for customized message
local _pauseMessage
_pauseMessage="${1:-Paused. Ready to continue?}"
if _seekConfirmation_ "${_pauseMessage}"; then
info "Continuing..."
else
notice "Exiting Script"
_safeExit_
fi
}
_printAnsi_() {
# DESC:
# Helps debug ansi escape sequence in text by displaying the escape codes
# ARGS:
# $1 (Required) String input with ansi escape sequence.
# OUTS:
# stdout: Ansi escape sequence printed in output as is.
# USAGE:
# _printAnsi_ "$(tput bold)$(tput setaf 9)Some Text"
# CREDIT:
# https://github.com/labbots/bash-utility/blob/master/src/debug.sh
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
#printf "%s\n" "$(tr -dc '[:print:]'<<<$1)"
printf "%s\n" "${1//$'\e'/\\e}"
}
_printArray_() {
# DESC:
# Prints the content of array as key value pairs for easier debugging. Only prints in verbose mode.
# ARGS:
# $1 (Required) - String variable name of the array
# $2 (Optional) - Line number where _printArray_ is called
# OUTS:
# stdout: Formatted key value of array
# USAGE:
# testArray=("1" "2" "3" "4")
# _printArray_ "testArray" ${LINENO}
# CREDIT:
# https://github.com/labbots/bash-utility/blob/master/src/debug.sh
[[ $# == 0 ]] && fatal "Missing required argument to ${FUNCNAME[0]}"
local _arrayName="${1}"
local _lineNumber="${2:-}"
declare -n _arr="${1}"
[[ ${VERBOSE:-} != true ]] && return 0
debug "Contents of \${${_arrayName}[@]}" "${_lineNumber}"
for _k in "${!_arr[@]}"; do
debug "${_k} = ${_arr[${_k}]}"
done
}