1 ## oils_failures_allowed: 4
2 ## compare_shells: bash
3
4
5 #### help
6 help
7 echo status=$? >&2
8 help help
9 echo status=$? >&2
10 help -- help
11 echo status=$? >&2
12 ## STDERR:
13 status=0
14 status=0
15 status=0
16 ## END
17
18 #### bad help topic
19 help ZZZ 2>$TMP/err.txt
20 echo "help=$?"
21 cat $TMP/err.txt | grep -i 'no help topics' >/dev/null
22 echo "grep=$?"
23 ## STDOUT:
24 help=1
25 grep=0
26 ## END
27
28 #### type -t -> function
29 f() { echo hi; }
30 type -t f
31 ## stdout: function
32
33 #### type -t -> alias
34 shopt -s expand_aliases
35 alias foo=bar
36 type -t foo
37 ## stdout: alias
38
39 #### type -t -> builtin
40 type -t echo read : [ declare local
41 ## STDOUT:
42 builtin
43 builtin
44 builtin
45 builtin
46 builtin
47 builtin
48 ## END
49
50 #### type -t -> keyword
51 type -t for time ! fi do {
52 ## STDOUT:
53 keyword
54 keyword
55 keyword
56 keyword
57 keyword
58 keyword
59 ## END
60
61 #### type -t control flow
62
63 # this differs from bash, but don't lie!
64 type -t break continue return exit
65 ## STDOUT:
66 keyword
67 keyword
68 keyword
69 keyword
70 ## END
71 ## OK bash STDOUT:
72 builtin
73 builtin
74 builtin
75 builtin
76 ## END
77
78
79 #### type -t -> file
80 type -t find xargs
81 ## STDOUT:
82 file
83 file
84 ## END
85
86 #### type -t doesn't find non-executable (like command -v)
87 PATH="$TMP:$PATH"
88 touch $TMP/non-executable
89 type -t non-executable
90 ## stdout-json: ""
91 ## status: 1
92 ## BUG bash STDOUT:
93 file
94 ## END
95 ## BUG bash status: 0
96
97 #### type -t -> not found
98 type -t echo ZZZ find =
99 echo status=$?
100 ## STDOUT:
101 builtin
102 file
103 status=1
104 ## END
105 ## STDERR:
106 ## END
107
108 #### type -> not found
109 type zz 2>err.txt
110 echo status=$?
111 grep -o 'not found' err.txt
112 ## STDOUT:
113 status=1
114 not found
115 ## END
116
117 #### type -p and -P builtin -> file
118 touch /tmp/{mv,tar,grep}
119 chmod +x /tmp/{mv,tar,grep}
120 PATH=/tmp:$PATH
121
122 type -p mv tar grep
123 echo --
124 type -P mv tar grep
125 ## STDOUT:
126 /tmp/mv
127 /tmp/tar
128 /tmp/grep
129 --
130 /tmp/mv
131 /tmp/tar
132 /tmp/grep
133 ## END
134
135 #### type -p builtin -> not found
136 type -p FOO BAR NOT_FOUND
137 ## status: 1
138 ## stdout-json: ""
139
140 #### type -p builtin -> not a file
141 type -p cd type builtin command
142 ## stdout-json: ""
143
144 #### type -P builtin -> not found
145 type -P FOO BAR NOT_FOUND
146 ## status: 1
147 ## stdout-json: ""
148
149 #### type -P builtin -> not a file
150 type -P cd type builtin command
151 ## stdout-json: ""
152 ## status: 1
153
154 #### type -P builtin -> not a file but file found
155 touch /tmp/{mv,tar,grep}
156 chmod +x /tmp/{mv,tar,grep}
157 PATH=/tmp:$PATH
158
159 mv () { ls; }
160 tar () { ls; }
161 grep () { ls; }
162 type -P mv tar grep cd builtin command type
163 ## status: 1
164 ## STDOUT:
165 /tmp/mv
166 /tmp/tar
167 /tmp/grep
168 ## END
169
170 #### type -f builtin -> not found
171 type -f FOO BAR NOT FOUND
172 ## status: 1
173
174 #### type -f builtin -> function and file exists
175 touch /tmp/{mv,tar,grep}
176 chmod +x /tmp/{mv,tar,grep}
177 PATH=/tmp:$PATH
178
179 mv () { ls; }
180 tar () { ls; }
181 grep () { ls; }
182 type -f mv tar grep
183 ## STDOUT:
184 /tmp/mv is a file
185 /tmp/tar is a file
186 /tmp/grep is a file
187 ## OK bash STDOUT:
188 mv is /tmp/mv
189 tar is /tmp/tar
190 grep is /tmp/grep
191 ## END
192
193 #### mapfile
194 type mapfile >/dev/null 2>&1 || exit 0
195 printf '%s\n' {1..5..2} | {
196 mapfile
197 echo "n=${#MAPFILE[@]}"
198 printf '[%s]\n' "${MAPFILE[@]}"
199 }
200 ## STDOUT:
201 n=3
202 [1
203 ]
204 [3
205 ]
206 [5
207 ]
208 ## END
209 ## N-I dash/mksh/zsh/ash stdout-json: ""
210
211 #### readarray (synonym for mapfile)
212 type readarray >/dev/null 2>&1 || exit 0
213 printf '%s\n' {1..5..2} | {
214 readarray
215 echo "n=${#MAPFILE[@]}"
216 printf '[%s]\n' "${MAPFILE[@]}"
217 }
218 ## STDOUT:
219 n=3
220 [1
221 ]
222 [3
223 ]
224 [5
225 ]
226 ## END
227 ## N-I dash/mksh/zsh/ash stdout-json: ""
228
229 #### mapfile (array name): arr
230 type mapfile >/dev/null 2>&1 || exit 0
231 printf '%s\n' {1..5..2} | {
232 mapfile arr
233 echo "n=${#arr[@]}"
234 printf '[%s]\n' "${arr[@]}"
235 }
236 ## STDOUT:
237 n=3
238 [1
239 ]
240 [3
241 ]
242 [5
243 ]
244 ## END
245 ## N-I dash/mksh/zsh/ash stdout-json: ""
246
247 #### mapfile (delimiter): -d delim
248 # Note: Bash-4.4+
249 type mapfile >/dev/null 2>&1 || exit 0
250 printf '%s:' {1..5..2} | {
251 mapfile -d : arr
252 echo "n=${#arr[@]}"
253 printf '[%s]\n' "${arr[@]}"
254 }
255 ## STDOUT:
256 n=3
257 [1:]
258 [3:]
259 [5:]
260 ## END
261 ## N-I dash/mksh/zsh/ash stdout-json: ""
262
263 #### mapfile (delimiter): -d '' (null-separated)
264 # Note: Bash-4.4+
265 type mapfile >/dev/null 2>&1 || exit 0
266 printf '%s\0' {1..5..2} | {
267 mapfile -d '' arr
268 echo "n=${#arr[@]}"
269 printf '[%s]\n' "${arr[@]}"
270 }
271 ## STDOUT:
272 n=3
273 [1]
274 [3]
275 [5]
276 ## END
277 ## N-I dash/mksh/zsh/ash stdout-json: ""
278
279 #### mapfile (truncate delim): -t
280 type mapfile >/dev/null 2>&1 || exit 0
281 printf '%s\n' {1..5..2} | {
282 mapfile -t arr
283 echo "n=${#arr[@]}"
284 printf '[%s]\n' "${arr[@]}"
285 }
286 ## STDOUT:
287 n=3
288 [1]
289 [3]
290 [5]
291 ## END
292 ## N-I dash/mksh/zsh/ash stdout-json: ""
293
294 #### mapfile -t doesn't remove \r
295 type mapfile >/dev/null 2>&1 || exit 0
296 printf '%s\r\n' {1..5..2} | {
297 mapfile -t arr
298 argv.py "${arr[@]}"
299 }
300 ## STDOUT:
301 ['1\r', '3\r', '5\r']
302 ## END
303 ## N-I dash/mksh/zsh/ash stdout-json: ""
304
305 #### mapfile (store position): -O start
306 type mapfile >/dev/null 2>&1 || exit 0
307 printf '%s\n' a{0..2} | {
308 arr=(x y z)
309 mapfile -O 2 -t arr
310 echo "n=${#arr[@]}"
311 printf '[%s]\n' "${arr[@]}"
312 }
313 ## STDOUT:
314 n=5
315 [x]
316 [y]
317 [a0]
318 [a1]
319 [a2]
320 ## END
321 ## N-I dash/mksh/zsh/ash stdout-json: ""
322
323 #### mapfile (input range): -s start -n count
324 type mapfile >/dev/null 2>&1 || exit 0
325 printf '%s\n' a{0..10} | {
326 mapfile -s 5 -n 3 -t arr
327 echo "n=${#arr[@]}"
328 printf '[%s]\n' "${arr[@]}"
329 }
330 ## STDOUT:
331 n=3
332 [a5]
333 [a6]
334 [a7]
335 ## END
336 ## N-I dash/mksh/zsh/ash stdout-json: ""
337
338 #### mapfile / readarray stdin TODO: Fix me.
339 shopt -s lastpipe # for bash
340
341 seq 2 | mapfile m
342 seq 3 | readarray r
343 echo ${#m[@]}
344 echo ${#r[@]}
345 ## STDOUT:
346 2
347 3
348 ## END