OILS / spec / toysh-posix.test.sh View on Github | oilshell.org

347 lines, 150 significant
1# toysh-posix
2
3#### Fatal error
4# http://landley.net/notes.html#20-06-2020
5
6abc=${a?bc} echo hello; echo blah
7## status: 1
8## OK yash/dash status: 2
9## stdout-json: ""
10
11#### setting readonly var (bash is only one where it's non-fatal)
12# http://landley.net/notes.html#20-06-2020
13
14readonly abc=123
15abc=def
16echo status=$?
17## status: 2
18## stdout-json: ""
19## OK osh/zsh status: 1
20## OK bash status: 0
21## BUG bash STDOUT:
22status=1
23## END
24
25#### readonly with temp binding
26# http://landley.net/notes.html#20-06-2020
27
28# temp binding
29readonly abc=123
30abc=def echo one
31echo status=$?
32
33echo potato < /does/not/exist || echo hello
34
35## status: 2
36## stdout-json: ""
37## OK osh/bash status: 0
38## OK osh/bash STDOUT:
39one
40status=0
41hello
42## END
43## OK zsh status: 1
44
45#### Failed redirect in assignment, vs. export
46
47abc=def > /does/not/exist1
48echo abc=$abc
49
50export abc=def > /does/not/exist2
51echo abc=$abc
52
53## STDOUT:
54abc=
55abc=
56## END
57## BUG bash STDOUT:
58abc=def
59abc=def
60## END
61## OK dash/mksh STDOUT:
62abc=
63## END
64## OK dash status: 2
65## OK mksh status: 1
66
67#### Evaluation order of redirect and ${undef?error}
68# http://landley.net/notes.html#12-06-2020
69rm *
70
71rm -f walrus
72$SH -c 'X=${x?bc} > walrus'
73if test -f walrus; then echo 'exists1'; fi
74
75rm -f walrus
76$SH -c '>walrus echo ${a?bc}'
77test -f walrus
78if test -f walrus; then echo 'exists2'; fi
79## STDOUT:
80exists1
81## END
82## OK bash stdout-json: ""
83
84#### Function def in pipeline
85# http://landley.net/notes.html#26-05-2020
86
87echo hello | potato() { echo abc; } | echo ha
88
89## STDOUT:
90ha
91## END
92
93#### dynamic glob - http://landley.net/notes.html#08-05-2020
94rm * # setup
95X='*'; echo $X
96echo "*"*".?z"
97## STDOUT:
98_tmp
99**.?z
100## END
101## BUG zsh status: 1
102## BUG zsh STDOUT:
103*
104## END
105
106#### no shebang
107rm *
108
109cat > snork << 'EOF'
110echo hello $BLAH
111EOF
112
113chmod +x snork
114$SH -c 'BLAH=123; ./snork'
115$SH -c 'BLAH=123; exec ./snork'
116$SH -c 'BLAH=123 exec ./snork'
117## STDOUT:
118hello
119hello
120hello 123
121## END
122
123
124#### IFS
125
126IFS=x; X=abxcd; echo ${X/bxc/g}
127
128X=a=\"\$a\"; echo ${X//a/{x,y,z}}
129
130## STDOUT:
131agd
132{ ,y,z="${ ,y,z"}
133## END
134## BUG zsh STDOUT:
135agd
136{x,y,z}="${x,y,z}"
137## END
138## N-I dash status: 2
139## N-I dash stdout-json: ""
140
141#### shift is fatal at top level?
142# http://landley.net/notes.html#08-04-2020
143
144# This makes a difference for zsh, but not for bash?
145#set -o posix
146
147$SH -c 'shift; echo hello'
148## STDOUT:
149hello
150## END
151## OK dash status: 2
152## OK mksh status: 1
153## OK dash/mksh stdout-json: ""
154
155#### var and func - http://landley.net/notes.html#19-03-2020
156potato() { echo hello; }
157potato=42
158echo $potato
159
160potato
161
162## STDOUT:
16342
164hello
165## END
166
167
168#### IFS - http://landley.net/notes.html#05-03-2020
169
170IFS=x
171chicken() { for i in "$@"; do echo =$i=; done;}
172chicken one abc dxf ghi
173
174echo ---
175myfunc() { "$SH" -c 'IFS=x; for i in $@; do echo =$i=; done' blah "$@"; }
176myfunc one "" two
177
178## STDOUT:
179=one=
180=abc=
181=d f=
182=ghi=
183---
184=one=
185==
186=two=
187## END
188## BUG dash STDOUT:
189=one=
190=abc=
191=d f=
192=ghi=
193---
194=one=
195=two=
196## END
197## BUG zsh status: 1
198## BUG zsh stdout-json: ""
199
200#### for loop parsing - http://landley.net/notes.html#04-03-2020
201
202$SH -c '
203for i
204in one two three
205do echo $i;
206done
207'
208echo $?
209
210$SH -c 'for i; in one two three; do echo $i; done'
211test $? -ne 0 && echo cannot-parse
212
213## STDOUT:
214one
215two
216three
2170
218cannot-parse
219## END
220
221#### Parsing $(( ))
222# http://landley.net/notes.html#15-03-2020
223$SH -c 'echo $((echo hello))'
224if test $? -ne 0; then echo fail; fi
225## stdout: fail
226
227#### IFS - http://landley.net/notes.html#15-02-2020 (TODO: osh)
228
229IFS=x; A=xabcxx; for i in $A; do echo =$i=; done
230
231unset IFS; A=" abc def "; for i in ""$A""; do echo =$i=; done
232
233## STDOUT:
234==
235=abc=
236==
237==
238=abc=
239=def=
240==
241## END
242## BUG zsh status: 1
243## BUG zsh stdout-json: ""
244
245#### IFS 2 (TODO: osh)
246this one appears different between osh and bash
247A=" abc def "; for i in ""x""$A""; do echo =$i=; done
248
249## STDOUT:
250=x=
251=abc=
252=def=
253==
254## END
255## BUG zsh status: 1
256## BUG zsh stdout-json: ""
257
258#### IFS 3
259IFS=x; X="onextwoxxthree"; y=$X; echo $y
260## STDOUT:
261one two three
262## END
263## BUG zsh STDOUT:
264onextwoxxthree
265## END
266
267#### IFS 4
268IFS=x
269cc() { echo =$*=; for i in $*; do echo -$i-; done;}; cc "" ""
270cc() { echo ="$*"=; for i in =$*=; do echo -$i-; done;}; cc "" ""
271## STDOUT:
272= =
273--
274=x=
275-=-
276-=-
277## END
278## BUG mksh/dash STDOUT:
279= =
280=x=
281-=-
282-=-
283## END
284## BUG yash STDOUT:
285= =
286--
287--
288=x=
289-=-
290-=-
291## END
292## BUG zsh STDOUT:
293= =
294## END
295## BUG zsh status: 1
296
297#### IFS 5
298cc() { for i in $*; do echo -$i-; done;}; cc "" "" "" "" ""
299cc() { echo =$1$2=;}; cc "" ""
300## STDOUT:
301==
302## END
303## BUG yash STDOUT:
304--
305--
306--
307--
308--
309==
310## END
311## BUG zsh status: 1
312## BUG zsh stdout-json: ""
313
314#### Can't parse extra }
315
316$SH -c 'for i in a"$@"b;do echo =$i=;done;}' 123 456 789
317## status: 2
318## OK bash/mksh/zsh status: 1
319## STDOUT:
320## END
321
322#### Command Sub Syntax Error
323# http://landley.net/notes.html#28-01-2020
324
325echo $(if true)
326echo $?
327echo $(false)
328echo $?
329## status: 2
330## OK mksh/zsh status: 1
331## stdout-json: ""
332## BUG bash status: 0
333## BUG bash STDOUT:
3341
335
3360
337## END
338
339
340#### Pipeline - http://landley.net/notes-2019.html#16-12-2019
341echo hello | { read i; echo $i;} | { read i; echo $i;} | cat
342echo hello | while read i; do echo -=$i=- | sed s/=/@/g ; done | cat
343## STDOUT:
344hello
345-@hello@-
346## END
347