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.

53 lines
1.1 KiB

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