Browse Source

better array functions

master
pvincent 3 years ago
parent
commit
2da771263e
  1. 20
      lib/functions.sh

20
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
}
##

Loading…
Cancel
Save