From 366cffaf5b4a5041c1eb4e4d4c69cac3e6dec004 Mon Sep 17 00:00:00 2001 From: labbots Date: Mon, 8 Jun 2020 14:02:58 +0100 Subject: [PATCH] Updated is_array check --- .gitignore | 1 + README.md | 9 +++++---- bin/generate_readme.sh | 8 +++++--- src/variable.sh | 19 +++++++++---------- 4 files changed, 20 insertions(+), 17 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ceeb05b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/tmp diff --git a/README.md b/README.md index 7c4fe86..5355350 100644 --- a/README.md +++ b/README.md @@ -130,7 +130,7 @@ git commit -m 'Added/updated bash-utility library.' **Note:** When cloning your repository, use `--recurse-submodules` flag to `git clone` command to install the git sub modules. ## Usage -Library functions can be used by simply sourcing the library script file to your own script. +Bash utility functions can be used by simply sourcing the library script file to your own script. To access all the functions within the bash-utility library, you could import the main bash file as follows. ```shell @@ -1742,19 +1742,20 @@ Functions for handling variables. ### variable::is_array() Check if given variable is array. +Note: Pass the variable name instead of value of the variable. #### Example ```bash -array=("a" "b" "c") -variable::is_array "${array[@]}" +arr=("a" "b" "c") +variable::is_array "arr" #Output 0 ``` #### Arguments -- **$1** (mixed): Value of variable to check. +- **$1** (string): name of the variable to check. #### Exit codes diff --git a/bin/generate_readme.sh b/bin/generate_readme.sh index a229eb2..2c2f34c 100755 --- a/bin/generate_readme.sh +++ b/bin/generate_readme.sh @@ -99,7 +99,7 @@ _setup_arguments() { printf "Provided directory for bash script files %s does not exist.\n" "${SOURCE_SCRIPT_DIR}" && exit 1 fi - re='^[0-9]+$' + declare re='^[0-9]+$' if ! [[ "${MINLEVEL}" =~ $re ]] || ! [[ "${MAXLEVEL}" =~ $re ]]; then echo "error: Not a number" >&2 exit 1 @@ -205,7 +205,7 @@ _generate_toc() { _insert_toc_to_file() { - declare source_markdown toc_text start_toc info_toc end_toc utext_ampersand utext_slash + declare source_markdown toc_text start_toc info_toc end_toc toc_block utext_ampersand utext_slash source_markdown="${1}" toc_text="${2}" start_toc="" @@ -239,7 +239,7 @@ _insert_toc_to_file() { } _process_toc() { - declare toc_temp_file source_markdown level + declare toc_temp_file source_markdown level toc_text source_markdown="${1}" toc_temp_file=$(_setup_tempfile) @@ -259,6 +259,8 @@ _process_toc() { } main() { + # export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }' + # set -x trap 'exit "$?"' INT TERM && trap 'exit "$?"' EXIT set -o errexit -o noclobber -o pipefail diff --git a/src/variable.sh b/src/variable.sh index 8dd1f2d..985c8a5 100644 --- a/src/variable.sh +++ b/src/variable.sh @@ -4,26 +4,25 @@ # @brief Functions for handling variables. # @description Check if given variable is array. +# Note: Pass the variable name instead of value of the variable. # # @example -# array=("a" "b" "c") -# variable::is_array "${array[@]}" +# arr=("a" "b" "c") +# variable::is_array "arr" # #Output # 0 # -# @arg $1 mixed Value of variable to check. +# @arg $1 string name of the variable to check. # # @exitcode 0 If input is array. # @exitcode 1 If input is not an array. variable::is_array() { - declare input=("$@") if [[ -z "${1}" ]]; then return 1 - elif [[ "${#input[@]}" -gt 1 ]]; then - return 0 else - return 1 + declare -p "${1}" 2> /dev/null | grep 'declare \-[aA]' > /dev/null && return 0 fi + return 1 } # @description Check if given variable is a number. @@ -95,7 +94,7 @@ variable::is_float() { # @exitcode 0 If input is a boolean. # @exitcode 1 If input is not a boolean. variable::is_bool() { - [[ "${1}" = true || "${1}" = false ]] && return 0 || return 1; + [[ "${1}" = true || "${1}" = false ]] && return 0 || return 1 } # @description Check if given variable is a true. @@ -110,7 +109,7 @@ variable::is_bool() { # @exitcode 0 If input is true. # @exitcode 1 If input is not true. variable::is_true() { - [[ "${1}" = true || "${1}" -eq 0 ]] && return 0 || return 1; + [[ "${1}" = true || "${1}" -eq 0 ]] && return 0 || return 1 } # @description Check if given variable is false. @@ -125,5 +124,5 @@ variable::is_true() { # @exitcode 0 If input is false. # @exitcode 1 If input is not false. variable::is_false() { - [[ "${1}" = false || "${1}" -eq 1 ]] && return 0 || return 1; + [[ "${1}" = false || "${1}" -eq 1 ]] && return 0 || return 1 }