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.

59 lines
1.3 KiB

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