File: bootcd-run.lib.tst2

package info (click to toggle)
bootcd 3.10%2Bnmu1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 508 kB
  • ctags: 128
  • sloc: sh: 3,935; makefile: 117
file content (271 lines) | stat: -rwxr-xr-x 7,746 bytes parent folder | download | duplicates (6)
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
#!/bin/sh
# bootcd-run.ib.tst2 - show all kind of examples of using interactive and run

RUNLIB=../bootcd/bootcd-run.lib
ERRLOG=/tmp/interactive.tst.log

. $RUNLIB

# adds $1 and $2 and prints result
add() 
{
  local a b c
  a=$1
  b=$2
  c=$(($a+$b))
  echo "$c"
}

# This is the simple exaple showed in bootcd-run.lib.tst1
simple_example()
{
  interactive -h "=== simple_example ===" "add 3 5" "add 2 5 # with comment" "add 7 3" "add 3 4"
}

# This shows how interactive can run non-interactive
# It is only interactive if called with option -i
non_interactive()
{
  local yes="-y"
  [ $# -ge 1 ] && [ "$1" = "-i" ] && yes=""
  interactive $yes -h "=== non_interactive ===" "add 3 5" "add 2 5 # with comment" "add 7 3" "add 3 4"
}

# The user can toggle the -i option interactive
toggle_interactive()
{
  interactive -h "=== toggle_interactive ===" "non_interactive <-i>"
}

# uses -e to display a variable dynamicly" \
cumulated()
{
  c=0
  interactive -h "=== cumulated ===" -e "c=\$\(add 3 5\)" "c=\$\(add \$c \$\(add 2 5\)\) \# with comment c=\<\$c\>" "c=\$\(add \$c \$\(add 7 3\)\)" "c=\$\(add \$c \$\(add 3 4\)\)"
}

# sometimes the things todo have to be calculated in more steps
with_eval()
{
  local todo=""
  todo="$todo \"add 3 5\" \"add 2 5 # with comment\""
  todo="$todo \"add 7 3\" \"add 3 4\""
  eval "interactive -h \"=== with_eval ===\" $todo"
}

# for large call trees it may be a good idea to display 
# a header of each level. The header should always contain
# all headers of the upper levels. The upper level header 
# is given as $1
with_head()
{
  local IAHEAD=$1
  interactive -h "=== $IAHEAD / with_head ===" "add 3 5" "add 2 5 # with comment" "add 7 3" "add 3 4"
}

with_run_and_interactive()
{
  local TODO="" mm=0

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_ignore $mm "exit=1"
  in_ignore $mm "^/proc/1/task"
  in_ignore $mm "find:.*Permission denied"
  in_stdout $mm "^/proc/.*"
  TODO="$TODO \"LC_ALL=C find /proc/1 # Show /proc/1 but not /proc/1/task\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_ignore $mm "exit=1"
  in_ignore $mm "^/proc/[^1].*"
  in_ignore $mm "^/proc/1$"
  in_ignore $mm "^/proc/1/[^t]"
  in_ignore $mm "find:.*Permission denied"
  in_stdout $mm "^/proc/1/t"
  TODO="$TODO \"LC_ALL=C find /proc/1 # Show /proc/1/task\""

  eval "interactive -h \"=== with_run_and_interactive ===\" -r $TODO"
}


fun()
{
  if [ "$(set | grep "^IAHEAD=")" ]; then
    IAHEAD="$IAHEAD/fun"
  else
    IAHEAD="main"
    n=0
  fi

  local TODO="" mm=0

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_norun $mm # you can not set global variables with run
  TODO="$TODO \" n=$(($n+1)) \# n=\<\\\$n\>\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_norun $mm # you can not set global variables with run
  TODO="$TODO \"n=$(($n+1))\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_ignore $mm ".*"
  TODO="$TODO \"echo \\\\\\\"error on stdout\\\\\\\"\; \# ignore errors\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_stdout $mm ".*"
  TODO="$TODO \"echo \\\\\\\"error on stdout\\\\\\\"\; \# ignore but display errors\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  TODO="$TODO \"echo \\\\\\\"error on stdout\\\\\\\"\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_stdout $mm ".*"
  TODO="$TODO \"echo \\\\\\\"bckslash:\\\\\\\\ quotedquote:\\\\\\\\\\\\\\\" semikolon:\;\\\\\\\"\; \# bckslash:\\\\\\\\ quote:\\\\\\\" ignore but display errors \(many characters\)\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_stdout $mm ".*"
  q6="\\\\\\"
  TODO="$TODO \"echo $q6\"bckslash:$q6\\ quotedquote:$q6$q6\\\" semikolon:\;$q6\"\; \# bckslash:$q6\\ quote:$q6\" ignore but display errors \(many characters\)\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_norun $mm # it is no good idea to run interactive with run
  TODO="$TODO \"fun\""

  eval "interactive -r -e -h \"=== \\\$IAHEAD n=\<\\\$n\> ===\" $TODO"
}


fun_without_run()
{
  if [ "$(set | grep "^IAHEAD=")" ]; then
    IAHEAD="$IAHEAD/fun_without_run"
  else
    IAHEAD="main"
    n=0
  fi

  local TODO=""

  TODO="$TODO \"n=$(($n+1)) \# n=\<\\\$n\>\""
  TODO="$TODO \"n=$(($n+1))\""
  TODO="$TODO \"echo \\\\\\\"print on stdout\\\\\\\"\; \# ignore errors\""

  TODO="$TODO \"echo \\\\\\\"bckslash:\\\\\\\\ quotedquote:\\\\\\\\\\\\\\\" semikolon:\;\\\\\\\"\; \# bckslash:\\\\\\\\ quote:\\\\\\\" ignore but display errors \(many characters\)\""

  q6="\\\\\\"
  TODO="$TODO \"echo $q6\"bckslash:$q6\\ quotedquote:$q6$q6\\\" semikolon:\;$q6\"\; \# bckslash:$q6\\ quote:$q6\" ignore but display errors \(many characters\)\""

  TODO="$TODO \"fun_without_run\""

  eval "interactive -e -h \"=== \\\$IAHEAD n=\<\\\$n\> ===\" $TODO"
}

t4()
{
  local IAHEAD="$IAHEAD/t4"

  local TODO="" mm=0

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  TODO="$TODO \"sh -c \\\"exit 0\\\"\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  TODO="$TODO \"sh -c \\\"exit 1\\\"\""

  eval "interactive -r -h \"=== \$IAHEAD ===\" $TODO"
}

t3()
{
  local IAHEAD="$IAHEAD/t3" 
  local TODO="" mm=0

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_norun $mm # it is no good idea to run interactive (t4) with run
  TODO="$TODO \"t4\""

  eval "interactive -r -e -h \"=== \\\$IAHEAD ===\" $TODO"
}

t2()
{
  local IAHEAD="$IAHEAD/t2"
  local TODO="" mm=0

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_norun $mm # it is no good idea to run interactive (t3) with run
  TODO="$TODO \"t3\""

  eval "interactive -r -h \"=== \$IAHEAD ===\" $TODO"
}

t1()
{
  local IAHEAD="t1"
  local TODO=""

  TODO="$TODO \"t2\""

  eval "interactive -h \"=== \$IAHEAD ===\" $TODO"
}

set_a_to_7()
{
  a=7
}

print_a()
{
  echo "--- a=<$a> ---"
}

set_print_a_1()
{
  local IAHEAD="set_print_a" 
  local TODO="" mm=0
  a=0

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_norun $mm # you can not set global variables with run
  TODO="$TODO \"set_a_to_7 \# a=\<\\\$a\>\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_stdout $mm "^--- .* ---$"
  TODO="$TODO \"print_a\""

  eval "interactive -r -e -h \"=== \\\$IAHEAD ===\" $TODO"
}

set_print_a_2()
{
  local IAHEAD="set_print_a" 
  local TODO="" mm=0
  a=0

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_norun $mm # you can not set global variables with run
  TODO="$TODO \"set_a_to_7\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_stdout $mm "^--- .* ---$"
  TODO="$TODO \"eval \\\"echo \\\\\\\"--- \\\$a ---\\\\\\\"\\\"\""

  mm=$(($mm+1)); eval local IGNORE${mm}=\"\"; eval local STDOUT${mm}=\"\"
  in_stdout $mm "^--- .* ---$"
  TODO="$TODO \"print_a\""

  eval "interactive -r -h \"=== \$IAHEAD ===\" $TODO"
}

interactive -h "=== bootcd-run.lib.tst2 ===" \
  "simple_example # This is the simple exaple showed in bootcd-run.lib.tst1" \
  "with_eval # uses variable todo to calculate steps" \
  "non_interactive # This runs non-interactive (uses -y)" \
  "with_head mainfunction # (usses -h)" \
  "toggle_interactive # The user can toggle the -i option interactive" \
  "cumulated # uses -e to display a variable dynamicly" \
  "with_run_and_interactive # checks stdout, stderr and exit code of each command" \
  "fun # uses -h -e -r" \
  "fun_without_run # uses -h -e" \
  "t1" \
  "set_print_a_1 # uses -r -e -h" \
  "set_print_a_2 # uses -r -h"