-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbashreturnarray
More file actions
executable file
·208 lines (164 loc) · 4.56 KB
/
bashreturnarray
File metadata and controls
executable file
·208 lines (164 loc) · 4.56 KB
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
#!/bin/bash
# Set a global array value to test variable lifetimes.
myarr=( sample global array )
## The next set of functions are called, directly or indirectly,
## by the primary functions
return_array_building_string()
{
local ESEP="$1"
# Change IFS to break ONLY at newlines to prevent
# breaks between words of multi-word array elements:
local IFS=$'\n'
local -a arr=(
"one"
"two words"
"three
lines of
words"
)
# echo "View array before sending back to caller:"
# printf "'%s'\n" "${arr[@]}"
# The magic occurs in the next two lines. Using the '*' instead
# of the '@' instructs BASH to use the first character of the IFS
# (which we also change below) between the elements.
IFS="${ESEP}"
echo "${arr[*]}"
}
generic_array_string_builder()
{
local IFS="$1"
echo "${*:2}"
}
return_array_building_string_with_generic_function()
{
local IFS="$1"
local -a arr=(
"one"
"two words"
"three
lines of
words"
)
generic_array_string_builder "${IFS}" "${arr[@]}"
}
reset_myarr()
{
myarr=( $( date +"%Y %m %d" ) )
}
demo_reset_by_name()
{
local -a myarr
reset_myarr
echo "In demo_reset_by_name(), after calling reset_myarr(), the elements of myarr are:"
printf "'%s'\n" "${myarr[@]}"
echo
}
reset_by_read()
{
target="$1"
arr=( $( date +"%Y %m %d" ) )
read -a "${target}" < <( echo "${arr[@]}" )
}
demo_reset_by_read()
{
local -a myarr
reset_by_read "myarr"
echo "In demo_reset_by_read(), after calling reset_by_read(), the elements of myarr are:"
printf "'%s'\n" "${myarr[@]}"
echo
}
reset_by_mapfile()
{
target="$1"
arr=( $( date +"%Y %m %d" ) )
# This construction does not work, result is no elements
# echo "${arr[@]}" | mapfile -t "${target}"
# This construction does not work, result is single element with spaces
# mapfile -t "${target}" < <( echo "${arr[@]}" )
# This is the proper construction.
local IFS=$'\n'
mapfile -t "${target}" < <( echo "${arr[*]}" )
}
demo_reset_by_mapfile()
{
local -a myarr
reset_by_mapfile "myarr"
echo "In demo_reset_by_mapfile(), after calling reset_by_mapfile(), the elements of myarr:"
printf "'%s'\n" "${myarr[@]}"
echo
}
## Following functions are the primary functions, called with a case
## statement sorting the input parameters on the command line:
simple_return_array_string()
{
local ESEP=$'\a'
# Uncomment the following to see what the returned string looks like:
# local arrstr=$( return_array_building_string "${ESEP}" )
# echo "arrstr = '${arrstr}'"
local IFS="${ESEP}"
local -a locarr=( $( return_array_building_string "${ESEP}" ) )
printf "'%s'\n\n" "${locarr[@]}"
}
generic_return_array_string()
{
local ESEP=$'\a'
local IFS="${ESEP}"
local -a locarr=( $( return_array_building_string_with_generic_function "${ESEP}" ) )
printf "'%s'\n\n" "${locarr[@]}"
}
fake_return_reset_by_name()
{
echo "In fake_return_reset_by_name(), the elements of myarr are:"
printf "'%s'\n" "${myarr[@]}"
echo
demo_reset_by_name
echo "Back in fake_return_reset_by_name(), the elements of myarr are:"
printf "'%s'\n" "${myarr[@]}"
echo
}
fake_return_reset_using_read()
{
echo "In fake_return_reset_using_read(), the elements of myarr are:"
printf "'%s'\n" "${myarr[@]}"
echo
demo_reset_by_read "myarr"
echo "Back in fake_return_reset_using_read(), the elements of myarr are:"
printf "'%s'\n" "${myarr[@]}"
echo
}
fake_return_reset_using_mapfile()
{
echo "In fake_return_reset_using_mapfile(), the elements of myarr:"
printf "'%s'\n" "${myarr[@]}"
echo
demo_reset_by_mapfile "myarr"
echo "Back in fake_return_reset_using_mapfile(), the elements of myarr:"
printf "'%s'\n" "${myarr[@]}"
echo
}
# echo "Original contents of global variable myarr:"
# printf "%s\n" "${myarr[@]}"
# echo
# demo_returning_array_strings
# demo_constructing_arrays_at_known_names
# echo
# echo "Confirm the global-scope array was not touched:"
# printf "%s\n" "${myarr[@]}"
prognum="$1"
case $prognum in
1) echo "simple_return_array_string()"
simple_return_array_string
;;
2) echo "generic_return_array_string()"
generic_return_array_string
;;
3) echo "fake_return_reset_by_name()"
fake_return_reset_by_name
;;
4) echo "fake_return_reset_using_read()"
fake_return_reset_using_read
;;
5) echo "fake_return_reset_using_mapfile()"
fake_return_reset_using_mapfile
;;
esac