You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
908 B
45 lines
908 B
|
|
##
|
|
# return 0 (true) if array (passed by name) contains element
|
|
# 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
|
|
>&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
|
|
}
|
|
|
|
##
|
|
#
|
|
askConfirmation () {
|
|
case "$1" in
|
|
y|Y|yes|YES )
|
|
QUESTION="(Y/n)?"
|
|
DEFAULT=0
|
|
;;
|
|
* )
|
|
QUESTION="(y/N)?"
|
|
DEFAULT=1
|
|
;;
|
|
esac
|
|
read -p "$QUESTION : " choice
|
|
case "$choice" in
|
|
y|Y|yes|YES ) return 0;; #true
|
|
n|no|N|NO ) return 1;; #false
|
|
* ) return $DEFAULT;;
|
|
esac
|
|
}
|