Guest User

Untitled

a guest
Apr 19th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.24 KB | None | 0 0
  1. arr=(1 2 3)
  2.  
  3. syntax error at line 8: `arr=' unexpected
  4.  
  5. set '1st element' 2 3 # setting the array
  6.  
  7. set -- "$@" more # adding elements to the end of the array
  8.  
  9. shift 2 # removing elements (here 2) from the beginning of the array
  10.  
  11. printf '<%s>n' "$@" # passing all the elements of the $@ array
  12. # as arguments to a command
  13.  
  14. for i do # looping over the elements of the $@ array ($1, $2...)
  15. printf 'Looping over "%s"n' "$i"
  16. done
  17.  
  18. printf '%sn' "$1" # accessing individual element of the array.
  19. # up to the 9th only with the Bourne shell though
  20. # (only the Bourne shell), and note that you need
  21. # the braces (as in "${10}") past the 9th in other
  22. # shells.
  23.  
  24. printf '%sn' "$# elements in the array"
  25.  
  26. printf '%sn' "$*" # join the elements of the array with the
  27. # first character (byte in some implementations)
  28. # of $IFS (not in the Bourne shell where it's on
  29. # space instead regardless of the value of $IFS)
  30.  
  31. #!/bin/sh
  32. # ARRAY.sh: example usage of arrays in Bourne Shell
  33.  
  34. array_traverse()
  35. {
  36. for i in $(seq 1 $2)
  37. do
  38. current_value=$1$i
  39. echo $(eval echo $$current_value)
  40. done
  41. return 1
  42. }
  43.  
  44. ARRAY_1=one
  45. ARRAY_2=two
  46. ARRAY_3=333
  47. array_traverse ARRAY_ 3
  48.  
  49. sentence="I don't need arrays because I can use delimited strings"
  50. for word in $sentence
  51. do
  52. printf '%sn' "$word"
  53. done
  54.  
  55. #!/bin/sh
  56.  
  57. ## The following functions implement vectors (arrays) operations in dash:
  58. ## Definition of a vector <v>:
  59. ## v_0 - variable that stores the number of elements of the vector
  60. ## v_1..v_n, where n=v_0 - variables that store the values of the vector elements
  61.  
  62. VectorAddElementNext () {
  63. # Vector Add Element Next
  64. # Adds the string contained in variable $2 in the next element position (vector length + 1) in vector $1
  65.  
  66. local elem_value
  67. local vector_length
  68. local elem_name
  69.  
  70. eval elem_value="$$2"
  71. eval vector_length=$$1_0
  72. if [ -z "$vector_length" ]; then
  73. vector_length=$((0))
  74. fi
  75.  
  76. vector_length=$(( vector_length + 1 ))
  77. elem_name=$1_$vector_length
  78.  
  79. eval $elem_name="$elem_value"
  80. eval $1_0=$vector_length
  81. }
  82.  
  83. VectorAddElementDVNext () {
  84. # Vector Add Element Direct Value Next
  85. # Adds the string $2 in the next element position (vector length + 1) in vector $1
  86.  
  87. local elem_value
  88. local vector_length
  89. local elem_name
  90.  
  91. eval elem_value="$2"
  92. eval vector_length=$$1_0
  93. if [ -z "$vector_length" ]; then
  94. vector_length=$((0))
  95. fi
  96.  
  97. vector_length=$(( vector_length + 1 ))
  98. elem_name=$1_$vector_length
  99.  
  100. eval $elem_name="$elem_value"
  101. eval $1_0=$vector_length
  102. }
  103.  
  104. VectorAddElement () {
  105. # Vector Add Element
  106. # Adds the string contained in the variable $3 in the position contained in $2 (variable or direct value) in the vector $1
  107.  
  108. local elem_value
  109. local elem_position
  110. local vector_length
  111. local elem_name
  112.  
  113. eval elem_value="$$3"
  114. elem_position=$(($2))
  115. eval vector_length=$$1_0
  116. if [ -z "$vector_length" ]; then
  117. vector_length=$((0))
  118. fi
  119.  
  120. if [ $elem_position -ge $vector_length ]; then
  121. vector_length=$elem_position
  122. fi
  123.  
  124. elem_name=$1_$elem_position
  125.  
  126. eval $elem_name="$elem_value"
  127. if [ ! $elem_position -eq 0 ]; then
  128. eval $1_0=$vector_length
  129. fi
  130. }
  131.  
  132. VectorAddElementDV () {
  133. # Vector Add Element
  134. # Adds the string $3 in the position $2 (variable or direct value) in the vector $1
  135.  
  136. local elem_value
  137. local elem_position
  138. local vector_length
  139. local elem_name
  140.  
  141. eval elem_value="$3"
  142. elem_position=$(($2))
  143. eval vector_length=$$1_0
  144. if [ -z "$vector_length" ]; then
  145. vector_length=$((0))
  146. fi
  147.  
  148. if [ $elem_position -ge $vector_length ]; then
  149. vector_length=$elem_position
  150. fi
  151.  
  152. elem_name=$1_$elem_position
  153.  
  154. eval $elem_name="$elem_value"
  155. if [ ! $elem_position -eq 0 ]; then
  156. eval $1_0=$vector_length
  157. fi
  158. }
  159.  
  160. VectorPrint () {
  161. # Vector Print
  162. # Prints all the elements names and values of the vector $1 on sepparate lines
  163.  
  164. local vector_length
  165.  
  166. vector_length=$(($1_0))
  167. if [ "$vector_length" = "0" ]; then
  168. echo "Vector "$1" is empty!"
  169. else
  170. echo "Vector "$1":"
  171. for i in $(seq 1 $vector_length); do
  172. eval echo "[$i]: \"$$1_$i\""
  173. ###OR: eval printf '%s\n' "[$i]: \"$$1_$i\""
  174. done
  175. fi
  176. }
  177.  
  178. VectorDestroy () {
  179. # Vector Destroy
  180. # Empties all the elements values of the vector $1
  181.  
  182. local vector_length
  183.  
  184. vector_length=$(($1_0))
  185. if [ ! "$vector_length" = "0" ]; then
  186. for i in $(seq 1 $vector_length); do
  187. unset $1_$i
  188. done
  189. unset $1_0
  190. fi
  191. }
  192.  
  193. ##################
  194. ### MAIN START ###
  195. ##################
  196.  
  197. ## Setting vector 'params' with all the parameters received by the script:
  198. for i in $(seq 1 $#); do
  199. eval param="${$i}"
  200. VectorAddElementNext params param
  201. done
  202.  
  203. # Printing the vector 'params':
  204. VectorPrint params
  205.  
  206. read temp
  207.  
  208. ## Setting vector 'params2' with the elements of the vector 'params' in reversed order:
  209. if [ -n "$params_0" ]; then
  210. for i in $(seq 1 $params_0); do
  211. count=$((params_0-i+1))
  212. VectorAddElement params2 count params_$i
  213. done
  214. fi
  215.  
  216. # Printing the vector 'params2':
  217. VectorPrint params2
  218.  
  219. read temp
  220.  
  221. ## Getting the values of 'params2'`s elements and printing them:
  222. if [ -n "$params2_0" ]; then
  223. echo "Printing the elements of the vector 'params2':"
  224. for i in $(seq 1 $params2_0); do
  225. eval current_elem_value="$params2_$i"
  226. echo "params2_$i="$current_elem_value""
  227. done
  228. else
  229. echo "Vector 'params2' is empty!"
  230. fi
  231.  
  232. read temp
  233.  
  234. ## Creating a two dimensional array ('a'):
  235. for i in $(seq 1 10); do
  236. VectorAddElement a 0 i
  237. for j in $(seq 1 8); do
  238. value=$(( 8 * ( i - 1 ) + j ))
  239. VectorAddElementDV a_$i $j $value
  240. done
  241. done
  242.  
  243. ## Manually printing the two dimensional array ('a'):
  244. echo "Printing the two-dimensional array 'a':"
  245. if [ -n "$a_0" ]; then
  246. for i in $(seq 1 $a_0); do
  247. eval current_vector_lenght=$a_$i_0
  248. if [ -n "$current_vector_lenght" ]; then
  249. for j in $(seq 1 $current_vector_lenght); do
  250. eval value="$a_$i_$j"
  251. printf "$value "
  252. done
  253. fi
  254. printf "n"
  255. done
  256. fi
  257.  
  258. ################
  259. ### MAIN END ###
  260. ################
Add Comment
Please, Sign In to add comment