From 00b17d8bc358c7a155b093d64c51e8e64a4220ba Mon Sep 17 00:00:00 2001 From: pvincent Date: Tue, 7 Apr 2020 16:29:54 +0400 Subject: [PATCH] bash functions as library functions --- lib/functions.sh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 lib/functions.sh diff --git a/lib/functions.sh b/lib/functions.sh new file mode 100644 index 0000000..a1124ce --- /dev/null +++ b/lib/functions.sh @@ -0,0 +1,35 @@ + +## +# return 0 (true) if array (passed by name) contains element +# usage: +# ARRAY = ( a b c ) +# 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 +} + +## +# +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 +} \ No newline at end of file