improved readabality

This commit is contained in:
pvincent
2024-03-05 23:29:55 +04:00
parent c9fd0aca3e
commit a06a05045e
7 changed files with 33 additions and 44 deletions
+12 -20
View File
@@ -3,30 +3,22 @@
##
# return 0 (true) if array (passed by name) contains element
# usage:
# ARRAY = ( a b c )
# 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