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.

44 lines
908 B

  1. ##
  2. # return 0 (true) if array (passed by name) contains element
  3. # usage:
  4. # ARRAY = ( a b c )
  5. # containsElement ARRAY 'a' => 0
  6. containsElement () {
  7. local -a 'arraykeys=("${!'"$1"'[@]}")'
  8. if $(isArray $1); then
  9. for index in ${arraykeys[*]}; do
  10. current=$1"[$index]"
  11. [[ "${!current}" == "$2" ]] && return 0; # found
  12. done
  13. return 1; # not found
  14. else
  15. >&2 echo "ERROR: $1 not an array!"
  16. return 2; # not an array
  17. fi
  18. }
  19. isArray(){
  20. [[ "$(declare -p $1 2> /dev/null)" =~ "declare -a" ]] && return 0; # is an array
  21. return 1; # not an array
  22. }
  23. ##
  24. #
  25. askConfirmation () {
  26. case "$1" in
  27. y|Y|yes|YES )
  28. QUESTION="(Y/n)?"
  29. DEFAULT=0
  30. ;;
  31. * )
  32. QUESTION="(y/N)?"
  33. DEFAULT=1
  34. ;;
  35. esac
  36. read -p "$QUESTION : " choice
  37. case "$choice" in
  38. y|Y|yes|YES ) return 0;; #true
  39. n|no|N|NO ) return 1;; #false
  40. * ) return $DEFAULT;;
  41. esac
  42. }