#!/bin/bash ## # return 0 (true) if array (passed by name) contains element # usage: # ARRAY=( a b c ) # containsElement ARRAY 'a' => 0 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 } ## return 0 (true) if arg1 (passed by name) is an array function isArray { [[ "$(declare -p "$1" 2>/dev/null)" =~ "declare -a" ]] || return 1 } function askConfirmation { case "$1" in y | Y | yes | YES) QUESTION="(Y/n)?" DEFAULT=0 ;; *) QUESTION="(y/N)?" DEFAULT=1 ;; esac read -rp "$QUESTION : " choice case "$choice" in y | Y | yes | YES) return 0 ;; #true n | no | N | NO) return 1 ;; #false *) return $DEFAULT ;; esac }