bashArrays.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Bash array fucntions
  2. #
  3. # Copyright (C) George Jones <gmj@pobox.com>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #
  18. #
  19. # * TODO LIST
  20. # ** Functions to add
  21. # -arraySplit
  22. # -arrayJoin
  23. # -arrayPrepend (special case of append? with switch/option? wrapper?)
  24. #
  25. # ** General TODOs
  26. # - Run validation tests, mark funcions as working or not, return 1 if not working.
  27. # - Figure out some way to add options/config, e.g. --noclobber for create, etc.
  28. #
  29. # http://www.linuxjournal.com/content/return-values-bash-functions
  30. #
  31. # Also see, c.a. 2022-10-18, very helpful
  32. #
  33. # https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
  34. source bashUtils.sh
  35. isArray() {
  36. # Test if variable is an array
  37. #
  38. # Usage: isArray NAME
  39. # Return: 0 if NAME is array, 1 otherwise.
  40. #
  41. # https://gist.github.com/coderofsalvation/8377369
  42. #
  43. export arrayError=""
  44. if [ -z "${1:-}" ]; then
  45. arrayError="isArray: Missing array name."
  46. return 1
  47. fi
  48. if [ -n "$BASH" ]; then
  49. # this is officially a hack, but it seems to be the best solution. Yuck.
  50. declare -p ${1:-''} 2> /dev/null | grep 'declare \-a' >/dev/null && return 0
  51. fi
  52. arrayError="isArray: ${1} is not an array."
  53. return 1
  54. }
  55. arrayCreate() {
  56. # Create an array.
  57. #
  58. # Usgae: arrayCreate NAME [element1 element2...]
  59. # Ouput: NAME set as global, $arrayErr set as error message
  60. # Return: 0 if defined, 1 otherwise.
  61. #
  62. export arrayError=""
  63. if [ -z "${1:-}" ]; then
  64. arrayError="arrayCreate: Missing array name."
  65. echo $arrayError 1>&2
  66. return 1
  67. fi
  68. local var=${1}
  69. shift
  70. if [ -z "${1:-}" ]; then
  71. foo="foo"
  72. eval "$var=( )"
  73. else
  74. eval "$var=( $@ )"
  75. fi
  76. return 0
  77. }
  78. arrayLength() {
  79. echo "Implment this. Reurn length"
  80. }
  81. #
  82. # void addelementtoarray (string <array_name>, string <element>, ...)
  83. #
  84. # http://www.linuxquestions.org/questions/programming-9/bash-array-add-function-example-using-indirect-array-reference-as-function-argument-815329/
  85. function arrayAppend {
  86. export arrayError=""
  87. if [ -z "${1:-}" ]; then
  88. arrayError="arrayAppend: Missing array name."
  89. return 1
  90. fi
  91. local R=$1
  92. shift
  93. echo R is $R
  94. status=isArray $R
  95. if [ ! $status ]; then
  96. arrayError="arrayAppend: $R is not an array "
  97. return 1
  98. fi
  99. echo $R is an array
  100. exit 1
  101. for A; do
  102. eval "$R[\${#$R[@]}]=\$A"
  103. done
  104. return 0
  105. # Or a one liner but more runtime expensive with large arrays since all elements will always expand.
  106. # In this method also, all element IDs are reset starting from 0.
  107. # Maybe this is also what you need since the IDs here does not need to be sorted. A problem may occur on the former if an ID exist that is higher than the number of elements.
  108. # Only that it resets all IDs everytime.
  109. # eval "$1=(\"\${$1[@]}\" \"${2:@}\")"
  110. }
  111. arrayTest() {
  112. # execute a test of array expected values
  113. #
  114. # Usage: arrayTest "test name" "expected results "actual results"
  115. # Output: pass/fail message to stdout
  116. # Return: 0 on pass, 1 otherwise
  117. [ -z "${1:-}" ] && echo "arrayTest: Missing test name." 1>&2 && return 1
  118. local testName="${1:-}"
  119. shift
  120. [ -z "${1:-}" ] && echo "arrayTest: Missing expected values." 1>&2 && return 1
  121. local expectedValues="${1:-}"
  122. shift
  123. [ -z "${1:-}" ] && echo "arrayTest: Missing actual values." 1>&2 && return 1
  124. local actualValues="${1:-}"
  125. shift
  126. if stringsEqual "${expectedValues-}" "${actualValues-}"; then
  127. echo pass: $testName
  128. return 0
  129. else
  130. echo diff expected values and actual values 1>&1
  131. diff -b <(echo "$expectedValues" ) <(echo "$actualValues")
  132. echo fail: $testName
  133. return 1
  134. fi
  135. }
  136. arraySplit() {
  137. echo "Implment this. Take char/regexp, split, return array"
  138. }
  139. arrayJoin() {
  140. echo "Implment this. Take char/regexp, rarray, join, return array"
  141. }
  142. arrayDestroy() {
  143. echo "Implenet this. unset an array."
  144. }