|
|
@ -5,28 +5,20 @@ |
|
|
|
# usage: |
|
|
|
# ARRAY=( a b c ) |
|
|
|
# containsElement ARRAY 'a' => 0 |
|
|
|
containsElement() { |
|
|
|
local -a 'arraykeys=("${!'"$1"'[@]}")' |
|
|
|
if $(isArray $1); then |
|
|
|
for index in ${arraykeys[*]}; do |
|
|
|
current=$1"[$index]" |
|
|
|
[[ "${!current}" == "$2" ]] && return 0 # found |
|
|
|
done |
|
|
|
return 1 # not found |
|
|
|
else |
|
|
|
echo >&2 "ERROR: $1 not an array!" |
|
|
|
return 2 # not an array |
|
|
|
fi |
|
|
|
function containsElement { |
|
|
|
isArray "$1" || (echo >&2 "ERROR: <$1> not an array!" && return 2) |
|
|
|
array_inter="$1[@]" |
|
|
|
array=("${!array_inter}") |
|
|
|
for i in "${array[@]}"; do [[ "$i" == "$2" ]] && return 0; done |
|
|
|
return 1 |
|
|
|
} |
|
|
|
|
|
|
|
isArray() { |
|
|
|
[[ "$(declare -p $1 2>/dev/null)" =~ "declare -a" ]] && return 0 # is an array |
|
|
|
return 1 # not an array |
|
|
|
## return 0 (true) if arg1 (passed by name) is an array |
|
|
|
function isArray { |
|
|
|
[[ "$(declare -p "$1" 2>/dev/null)" =~ "declare -a" ]] || return 1 |
|
|
|
} |
|
|
|
|
|
|
|
## |
|
|
|
# |
|
|
|
askConfirmation() { |
|
|
|
function askConfirmation { |
|
|
|
case "$1" in |
|
|
|
y | Y | yes | YES) |
|
|
|
QUESTION="(Y/n)?" |
|
|
@ -37,7 +29,7 @@ askConfirmation() { |
|
|
|
DEFAULT=1 |
|
|
|
;; |
|
|
|
esac |
|
|
|
read -p "$QUESTION : " choice |
|
|
|
read -rp "$QUESTION : " choice |
|
|
|
case "$choice" in |
|
|
|
y | Y | yes | YES) return 0 ;; #true |
|
|
|
n | no | N | NO) return 1 ;; #false |
|
|
|