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.

38 lines
840 B

8 months ago
8 months ago
8 months ago
8 months ago
8 months ago
8 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 askConfirmation {
  19. case "$1" in
  20. y | Y | yes | YES)
  21. QUESTION="(Y/n)?"
  22. DEFAULT=0
  23. ;;
  24. *)
  25. QUESTION="(y/N)?"
  26. DEFAULT=1
  27. ;;
  28. esac
  29. read -rp "$QUESTION : " choice
  30. case "$choice" in
  31. y | Y | yes | YES) return 0 ;; #true
  32. n | no | N | NO) return 1 ;; #false
  33. *) return $DEFAULT ;;
  34. esac
  35. }