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.

46 lines
964 B

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