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.

54 lines
1.2 KiB

  1. # TODO: remove, leave it in miaou-server/lib/functions.sh only!
  2. # idempotent apt install [package1 package2 ...]
  3. function idem_apt_install() {
  4. for i in $@; do
  5. if ! (/usr/bin/dpkg-query --status "$i" >/dev/null 2>&1); then
  6. sudo apt install -y "$i"
  7. fi
  8. done
  9. }
  10. ##
  11. # return 0 (true) if array (passed by name) contains element
  12. # usage:
  13. # ARRAY = ( a b c )
  14. # containsElement ARRAY 'a' => 0
  15. containsElement() {
  16. local -a 'arraykeys=("${!'"$1"'[@]}")'
  17. if $(isArray $1); then
  18. for index in ${arraykeys[*]}; do
  19. current=$1"[$index]"
  20. [[ "${!current}" == "$2" ]] && return 0 # found
  21. done
  22. return 1 # not found
  23. else
  24. echo >&2 "ERROR: $1 not an array!"
  25. return 2 # not an array
  26. fi
  27. }
  28. isArray() {
  29. [[ "$(declare -p $1 2>/dev/null)" =~ "declare -a" ]] && return 0 # is an array
  30. return 1 # not an array
  31. }
  32. ##
  33. #
  34. askConfirmation() {
  35. case "$1" in
  36. y | Y | yes | YES)
  37. QUESTION="(Y/n)?"
  38. DEFAULT=0
  39. ;;
  40. *)
  41. QUESTION="(y/N)?"
  42. DEFAULT=1
  43. ;;
  44. esac
  45. read -p "$QUESTION : " choice
  46. case "$choice" in
  47. y | Y | yes | YES) return 0 ;; #true
  48. n | no | N | NO) return 1 ;; #false
  49. *) return $DEFAULT ;;
  50. esac
  51. }