-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy patharray_practice.sh
executable file
·353 lines (307 loc) · 11.3 KB
/
array_practice.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/env bash
# This file is part of eRCaGuy_hello_world: https://github.com/ElectricRCAircraftGuy/eRCaGuy_hello_world
# Run command:
# ./array_practice.sh
# References:
# 1. *****[VERY IMPORTANT AND USEFUL ANSWER!] https://stackoverflow.com/a/70572787/4561887
# 1. my own memory
# 1. https://github.com/ElectricRCAircraftGuy/PDF2SearchablePDF/blob/master/pdf2searchablepdf.sh
# 1. back_up_all_input_args.sh
# 1. combining (merging) arrays in bash: https://stackoverflow.com/a/38579195/4561887
# 1. *****EXCELLENT BASH ARRAYS TUTORIAL!
# https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
# 1. [my own answer] bash slicing and array indexing demo:
# https://unix.stackexchange.com/a/664956/114401
# Test multiple ways to build and print regular bash "indexed" arrays
# ----------------------------------------
# -1. See if you can have empty arrays (arrays of size 0 elements)
# Answer: yes! Run this example.
# ----------------------------------------
a1=("") # array of size 1 element, which is an empty string
a2=(a b "${a1[@]}" c)
echo "${a2[@]}"
echo "number of elements = ${#a2[@]}" # 4
a1=() # array of size 0 elements
a2=(a b "${a1[@]}" c)
echo "${a2[@]}"
echo "number of elements = ${#a2[@]}" # 3; there you have it! You CAN have arrays of zero elements!
echo ""
# ----------------------------------------
# 0. Array splicing (AKA: joining, combining, merging, etc.)
# NB: Bash does *not* have nested nor multidimensional arrays, so this is simply *combining* arrays!
# See: https://stackoverflow.com/a/12317983/4561887
# ----------------------------------------
a1=(a b c d)
a2=(d e f g)
a3=(1 2 3 "${a1[@]}" 4 5 "${a2[@]}" 6 7)
echo "${a1[@]}" # output: a b c d
echo "${a2[@]}" # output: d e f g
echo "${a3[@]}" # output: 1 2 3 a b c d 4 5 d e f g 6 7
echo ""
# ----------------------------------------
# 1. Build up one element at-a-time
# ----------------------------------------
my_array=()
my_array+=("element 1")
my_array+=("element 2")
my_array+=("element 3")
my_array_len=${#my_array[@]}
echo "Number of elements = $my_array_len"
echo "my_array contains:"
for i in "${my_array[@]}"; do
echo " $i"
done
echo ""
# ----------------------------------------
# 2. Build up all at once
# ----------------------------------------
my_array2=("element 1" "element 2" "element 3")
my_array2_len=${#my_array2[@]}
echo "Number of elements = $my_array2_len"
echo "my_array2 contains (using loop technique 1: range-based for loop):"
for element in "${my_array2[@]}"; do
echo " $element"
done
echo ""
# Alternative `for` loop! Iterate using indices to obtain the array elements, instead of iterating
# over elements directly using the "range-based for loop" as shown above.
echo "my_array2 contains (using loop technique 2: C-style index-based for loop):"
echo "- See: https://stackoverflow.com/a/356154/4561887 and "
echo " [better] https://www.cyberciti.biz/faq/bash-for-loop-array/"
echo " and [my answer where I used this] https://stackoverflow.com/a/70670852/4561887"
for (( i=0; i<"$my_array2_len"; i++ )); do
echo ' ${my_array2['"$i"']} = '"${my_array2[$i]}"
done
echo ""
# ----------------------------------------
# 3. Build the array all at once, but use a different format from above,
# specifying the elements on multiple lines.
# ----------------------------------------
my_array3=(
"element A"
"element B"
"element C"
"element D"
)
# yet another way to print the array: print using the array's **indices**
echo "Number of elements = ${#my_array3[@]}"
echo "my_array3 contains (by printing via its indices):"
for i in "${!my_array3[@]}"; do
echo " ${my_array3["$i"]}"
done
echo ""
# Now use an array as the input arguments to another command! For this demo, let the command be
# `ls`. Here is an array of arguments to this command:
my_args_array=("-a" "-l" "-F" "some path")
echo "========================================================================"
echo 'OPTION 1: pass the contents of the array as arguments to our command \
via the input array, "$@", which we set from our custom array'
echo "========================================================================"
# See: https://stackoverflow.com/a/70572787/4561887
# First, show what is in the magic input args array, `"$@"`:
echo '$@ = '"$@"
echo '$1 = '"$1"
echo '$2 = '"$2"
echo '$3 = '"$3"
echo '$4 = '"$4"
# Now, set it to what's in `my_args_array`
set -- "${my_args_array[@]}"
# And prove it is set to this
echo '$@ = '"$@"
echo '$1 = '"$1"
echo '$2 = '"$2"
echo '$3 = '"$3"
echo '$4 = '"$4"
# Now call the cmd with these arguments!
ls "$@"
echo "========================================================================"
echo 'OPTION 2: pass the contents of the array as arguments to our command \
via our custom array directly, "${my_args_array[@]}"'
echo "========================================================================"
ls "${my_args_array[@]}"
echo ""
echo "========================================================================"
echo "array summation"
echo "========================================================================"
array1=("1" "2" "3" "4")
array2=("5" "6" "7" "8")
array3=("${array1[@]}" "${array2[@]}") # see: https://stackoverflow.com/a/38579195/4561887
array3_len=${#array3[@]}
echo ""
echo "Number of elements = $array3_len"
echo "array3 contains:"
for i in "${array3[@]}"; do
echo " $i"
done
# slightly more-complicated example with multiple arrays plus some more individual elements added;
# this is very useful for building up an arguments array to pass to a command!
array4=("${array1[@]}" "100" "101" "${array2[@]}" "102")
array4_len=${#array4[@]}
echo ""
echo "Number of elements = $array4_len"
echo "array4 contains:"
for i in "${array4[@]}"; do
echo " $i"
done
echo ""
echo "========================================================================"
echo "array indexing (into array4)"
echo "========================================================================"
# - See this tutorial for help:
# https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
# - and my own answer on bash array indexing and slicing too:
# https://unix.stackexchange.com/a/664956/114401
echo "index 0: ${array4[0]}"
echo "index 1: ${array4[1]}"
echo "index 2: ${array4[2]}"
echo "index 3: ${array4[3]}"
echo "index 4: ${array4[4]}"
echo "index 5: ${array4[5]}"
echo "index 6: ${array4[6]}"
echo "index 7: ${array4[7]}"
echo "index 8: ${array4[8]}"
echo "index 9: ${array4[9]}"
echo "index 10: ${array4[10]}"
echo "index 11: ${array4[11]}" # intentionally out-of-bounds
echo ""
echo "index -1 ( 1st element from the right): ${array4[-1]}"
echo "index -2 ( 2nd element from the right): ${array4[-2]}"
echo "index -3 ( 3rd element from the right): ${array4[-3]}"
echo "index -4 ( 4th element from the right): ${array4[-4]}"
echo "index -5 ( 5th element from the right): ${array4[-5]}"
echo "index -6 ( 6th element from the right): ${array4[-6]}"
echo "index -7 ( 7th element from the right): ${array4[-7]}"
echo "index -8 ( 8th element from the right): ${array4[-8]}"
echo "index -9 ( 9th element from the right): ${array4[-9]}"
echo "index -10 (10th element from the right): ${array4[-10]}"
echo "index -11 (11th element from the right): ${array4[-11]}"
echo "index -12 (12th element from the right): ${array4[-12]}" # intentionally out-of-bounds
echo ""
echo "all elements: ${array4[@]}"
echo ""
echo "all array indices: ${!array4[@]}"
echo ""
echo "array size (number of elements): ${#array4[@]}"
echo ""
echo "Bash array slicing:"
echo '${array4[@]:3:5} (starting at index 3, retrieve 5 elements): '"${array4[@]:3:5}"
echo ""
# SAMPLE OUTPUT:
#
# eRCaGuy_hello_world/bash$ ./array_practice.sh
# Number of elements = 3
# my_array contains:
# element 1
# element 2
# element 3
#
# Number of elements = 3
# my_array2 contains (using loop technique 1: range-based for loop):
# element 1
# element 2
# element 3
#
# my_array2 contains (using loop technique 2: C-style index-based for loop):
# - See: https://stackoverflow.com/a/356154/4561887 and
# [better] https://www.cyberciti.biz/faq/bash-for-loop-array/
# and [my answer where I used this] https://stackoverflow.com/a/70670852/4561887
# ${my_array2[0]} = element 1
# ${my_array2[1]} = element 2
# ${my_array2[2]} = element 3
#
# Number of elements = 4
# my_array3 contains (by printing via its indices):
# element A
# element B
# element C
# element D
#
# ========================================================================
# OPTION 1: pass the contents of the array as arguments to our command \
# via the input array, "$@", which we set from our custom array
# ========================================================================
# $@ =
# $1 =
# $2 =
# $3 =
# $4 =
# $@ = -a -l -F some path
# $1 = -a
# $2 = -l
# $3 = -F
# $4 = some path
# ls: cannot access 'some path': No such file or directory
# ========================================================================
# OPTION 2: pass the contents of the array as arguments to our command \
# via our custom array directly, "${my_args_array[@]}"
# ========================================================================
# ls: cannot access 'some path': No such file or directory
#
# ========================================================================
# array summation
# ========================================================================
#
# Number of elements = 8
# array3 contains:
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8
#
# Number of elements = 11
# array4 contains:
# 1
# 2
# 3
# 4
# 100
# 101
# 5
# 6
# 7
# 8
# 102
#
# ========================================================================
# array indexing (into array4)
# ========================================================================
# index 0: 1
# index 1: 2
# index 2: 3
# index 3: 4
# index 4: 100
# index 5: 101
# index 6: 5
# index 7: 6
# index 8: 7
# index 9: 8
# index 10: 102
# index 11:
#
# index -1 ( 1st element from the right): 102
# index -2 ( 2nd element from the right): 8
# index -3 ( 3rd element from the right): 7
# index -4 ( 4th element from the right): 6
# index -5 ( 5th element from the right): 5
# index -6 ( 6th element from the right): 101
# index -7 ( 7th element from the right): 100
# index -8 ( 8th element from the right): 4
# index -9 ( 9th element from the right): 3
# index -10 (10th element from the right): 2
# index -11 (11th element from the right): 1
# ./array_practice.sh: line 185: array4: bad array subscript
# index -12 (12th element from the right):
#
# all elements: 1 2 3 4 100 101 5 6 7 8 102
#
# all array indices: 0 1 2 3 4 5 6 7 8 9 10
#
# array size (number of elements): 11
#
# Bash array slicing:
# ${array4[@]:3:5} (starting at index 3, retrieve 5 elements): 4 100 101 5 6
#