mirror of
https://github.com/armbian/bash-util.git
synced 2026-01-06 10:37:49 -08:00
Updated is_array check
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/tmp
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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="<!-- START ${SCRIPT_FILE} generated TOC please keep comment here to allow auto update -->"
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user