-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_func_dissect
executable file
·271 lines (202 loc) · 4.33 KB
/
test_func_dissect
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
#!/usr/bin/env bash
source mod_common
bi_enable
test_connection()
{
cat <<EOF
CONNECTION
A CONNECTION struct is the means by which the Bash interpreter
accesses consecutive statements.
EOF
return 0
echo "This function, when dissected, will show how"
echo "Consecutive statements are linked through"
echo "instances of CONNECTION structs."
}
test_case_com()
{
local -i retval="${1:-0}"
cat <<EOF
CASE_COM
The CASE_COM struct contains information to perform a Bash Case statement.
EOF
return 0
case "$SHLVL" in
0 ) echo "Root level" ;;
1 ) echo "1 level deep" ;;
2 ) echo "2 levels deep" ;;
*) echo "Too deep" ;;
esac
}
test_for_com()
{
cat <<EOF
FOR_COM
The FOR_COM struct contains information to perform a Bash for command
There are two *for* loop models:
1. Walk through a collection of values:
for val in "${array[@]}"
2. C-language-style controlled loop:
for (( ndx=0; ndx<10; ++ndx ))
EOF
return 0
local val
local -i count=0
for val in "$@"; do
printf "%2d: %s\n" $(( ++count )) "$val"
done
for (( count=0; count<10; ++count )); do
printf "%2d\n" "$count"
done
}
test_arith_for_com()
{
cat <<EOF
ARITH_FOR_COM
Although this is inherently tested in 'test_for_com', this
explicitly targets the ARITH_FOR_COM command type.
EOF
return 0
for (( count=0; count<10; ++count )); do
printf "%2d\n" "$count"
done
}
test_select_com()
{
cat <<EOF
SELECT_COM
This unusual command produces a kind of selection dialog.
EOF
return 0
local person
select person in jane robert olivia justin kayla robert; do
if [ -z "$person" ]; then
continue
fi
echo "You selected '$person'"
break
done
}
test_if_com()
{
cat <<EOF
IF_COM
This struct contains a *test* command to determine if true or false,
and commands for each outcome.
EOF
return 0
if [ "$USER" == "root" ]; then
echo "You are running as 'root'"
else
echo "You are running as '$USER'"
fi
}
test_while_com()
{
cat <<EOF
WHILE_COM
This struct contains a *test* command to determine if true or false,
and a command to perform when the *test* returns TRUE
EOF
return 0
local -i ndx=0
while [ "$ndx" -lt 10 ]; do
(( --ndx ))
done
}
test_arith_com()
{
cat <<EOF
ARITH_COM
This struct is used for arithmetic statements.
EOF
return 0
local -i left right
echo $(( right=10, left=right*10 ))
}
test_cond_com()
{
cat <<EOF
COND_COM
This struct is part of a condition evaluation tree. The generation
if this struct is triggered with the [[ ]] notation.
EOF
return 0
[[ "$USER" =~ rct ]]
}
test_simple_com()
{
cat <<EOF
SIMPLE_COM
This structure mainly consists of a WORD_LIST that contains both the
name of a command to run and the arguments to send to the command.
EOF
return 0
printf $'\x1b[34;1m%s\x1b[m is the name, at shell level %d.\n' "$user" "$SHLVL"
}
test_function_def()
{
cat <<EOF
FUNCTION_DEF
This command is generated for the body of a function. It consists of a
*name* member and an execution tree *command*.
EOF
return 0
inner_func()
{
echo "Hello from the inner func."
}
}
test_group_com()
{
cat <<EOF
GROUP_COM
A group is a set of statements contained by curly braces, and the
GROUP_COM structure contains only a flag and a pointer to a *command*.
EOF
return 0
{
echo "This is printout from an anonymous group."
echo "(Don't be alarmed!)"
}
}
test_subsell_com()
{
cat <<EOF
SUBSHELL_COM
I haven't been able to trigger the creation of this type of command
struct.
EOF
return 1
}
test_coproc_com()
{
cat <<EOF
COPROC_COM
I haven't been able to trigger the create of this type of command
struct.
EOF
return 1
}
read -n1 -p "Press a key to start tests."
test_arith_com
run_survey()
{
local OIFS="$IFS"
local IFS=$'\n'
local -a funcs=( $( declare -F | grep -o test_[^[:space:]]\\+ ) )
IFS="$OIFS"
local test
for test in "${funcs[@]}"; do
echo -n $'\e[2J\e[1;1H'
if "$test"; then
func_dissect "$test"
fi
echo
read -n1 -p "Press 'q' to quit, any other key to continue."
if [ "$REPLY" == 'q' ]; then
break;
fi
done
}
run_survey