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.

55 lines
1.2 KiB

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