From 2da771263e1e6f61f32c07945ad1d92e3b425d6b Mon Sep 17 00:00:00 2001 From: pvincent Date: Tue, 17 Aug 2021 16:14:55 +0400 Subject: [PATCH] better array functions --- lib/functions.sh | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/functions.sh b/lib/functions.sh index a1124ce..e3c3dd3 100644 --- a/lib/functions.sh +++ b/lib/functions.sh @@ -6,11 +6,21 @@ # containsElement ARRAY 'a' => 0 containsElement () { local -a 'arraykeys=("${!'"$1"'[@]}")' - for index in ${arraykeys[*]}; do - current=$1"[$index]" - [[ "${!current}" == "$2" ]] && return 0; # found - done - return 1; # not found + if $(isArray $1); then + for index in ${arraykeys[*]}; do + current=$1"[$index]" + [[ "${!current}" == "$2" ]] && return 0; # found + done + return 1; # not found + else + >&2 echo "ERROR: $1 not an array!" + return 2; # not an array + fi +} + +isArray(){ + [[ "$(declare -p $1 2> /dev/null)" =~ "declare -a" ]] && return 0; # is an array + return 1; # not an array } ##