MIAOU-BASH is a collection of settings and helpers for leveraging BASH. Developer-friendly, it may be used as solo package with or without the miaou project.
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.

50 lines
1020 B

9 months ago
9 months ago
9 months ago
9 months ago
7 months ago
9 months ago
9 months 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. function containsElement {
  8. isArray "$1" || (echo >&2 "ERROR: <$1> not an array!" && return 2)
  9. array_inter="$1[@]"
  10. array=("${!array_inter}")
  11. for i in "${array[@]}"; do [[ "$i" == "$2" ]] && return 0; done
  12. return 1
  13. }
  14. ## return 0 (true) if arg1 (passed by name) is an array
  15. function isArray {
  16. [[ "$(declare -p "$1" 2>/dev/null)" =~ "declare -a" ]] || return 1
  17. }
  18. function isDebian {
  19. grep ^ID=debian /etc/os-release
  20. }
  21. function isArch {
  22. grep ^ID=arch /etc/os-release
  23. }
  24. function os-release {
  25. grep ^ID= /etc/os-release | cut -d '=' -f2
  26. }
  27. function askConfirmation {
  28. case "$1" in
  29. y | Y | yes | YES)
  30. QUESTION="(Y/n)?"
  31. DEFAULT=0
  32. ;;
  33. *)
  34. QUESTION="(y/N)?"
  35. DEFAULT=1
  36. ;;
  37. esac
  38. read -rp "$QUESTION : " choice
  39. case "$choice" in
  40. y | Y | yes | YES) return 0 ;; #true
  41. n | no | N | NO) return 1 ;; #false
  42. *) return $DEFAULT ;;
  43. esac
  44. }