OILS / spec / sh-usage.test.sh View on Github | oils.pub

441 lines, 200 significant
1## tags: interactive
2## compare_shells: bash dash mksh zsh
3## oils_failures_allowed: 0
4
5#### sh -c
6$SH -c 'echo hi'
7## stdout: hi
8## status: 0
9
10#### empty -c input
11# had a bug here
12$SH -c ''
13## stdout-json: ""
14## status: 0
15
16#### sh +c is accepted
17$SH +c 'echo hi'
18## stdout: hi
19## status: 0
20## N-I mksh/yash stdout-json: ""
21## N-I mksh/yash status: 127
22
23#### empty stdin
24# had a bug here
25echo -n '' | $SH
26## stdout-json: ""
27## status: 0
28
29#### sh - and sh -- stop flag processing
30case $SH in zsh) exit ;; esac
31
32echo 'echo foo' > foo.sh
33
34$SH -x -v -- foo.sh
35
36echo -
37echo - >& 2
38
39$SH -x -v - foo.sh
40
41## STDOUT:
42foo
43-
44foo
45## END
46## STDERR:
47echo foo
48+ echo foo
49-
50echo foo
51+ echo foo
52## END
53
54# I think it turns off -x -v with -
55## BUG-2 mksh STDERR:
56echo foo
57+ echo foo
58-
59## END
60
61# set -o verbose not implemented for now
62## OK osh STDERR:
63+ echo foo
64-
65+ echo foo
66## END
67
68## BUG zsh STDOUT:
69## END
70## BUG zsh STDERR:
71## END
72
73#### shell obeys --help (regression for OSH)
74n=$($SH --help | wc -l)
75if test $n -gt 0; then
76 echo yes
77fi
78## STDOUT:
79yes
80## END
81## N-I dash/mksh stdout-json: ""
82
83#### args are passed
84$SH -c 'argv.py "$@"' dummy a b
85## stdout: ['a', 'b']
86
87#### args that look like flags are passed after script
88script=$TMP/sh1.sh
89echo 'argv.py "$@"' > $script
90chmod +x $script
91$SH $script --help --help -h
92## stdout: ['--help', '--help', '-h']
93
94#### args that look like flags are passed after -c
95$SH -c 'argv.py "$@"' --help --help -h
96## stdout: ['--help', '-h']
97
98#### exit with explicit arg
99exit 42
100## status: 42
101
102#### exit with no args
103false
104exit
105## status: 1
106
107#### --rcfile in non-interactive shell prints warnings
108echo 'echo rc' > rc
109
110$SH --rcfile rc -i </dev/null 2>interactive.txt
111grep -q 'warning' interactive.txt
112echo warned=$? >&2
113
114$SH --rcfile rc </dev/null 2>non-interactive.txt
115grep -q 'warning' non-interactive.txt
116echo warned=$? >&2
117
118head *interactive.txt
119
120## STDERR:
121warned=1
122warned=0
123## END
124## N-I bash/dash/mksh/zsh STDERR:
125warned=1
126warned=1
127## END
128
129#### accepts -l flag
130$SH -l -c 'exit 0'
131## status: 0
132
133
134#### accepts --login flag (dash and mksh don't accept long flags)
135$SH --login -c 'exit 0'
136## status: 0
137## OK dash status: 2
138## OK mksh status: 1
139
140
141#### osh --eval
142case $SH in bash|dash|mksh|zsh) exit ;; esac
143
144echo 'echo one "$@"' > one.sh
145echo 'echo fail "$@"; ( exit 42 )' > fail.sh
146
147$SH --eval one.sh \
148 -c 'echo status=$? flag -c "$@"' dummy x y z
149echo
150
151# Even though errexit is off, the shell exits if the last status of an --eval
152# file was non-zero.
153
154$SH --eval one.sh --eval fail.sh \
155 -c 'echo status=$? flag -c "$@"' dummy x y z
156echo status=$?
157
158## STDOUT:
159one x y z
160status=0 flag -c x y z
161
162one x y z
163fail x y z
164status=42 flag -c x y z
165status=0
166## END
167
168## N-I bash/dash/mksh/zsh STDOUT:
169## END
170
171#### Set LC_ALL LC_CTYPE LC_COLLATE LANG - affects glob ?
172
173# note: test/spec-common.sh sets LC_ALL
174unset LC_ALL
175
176touch _x_ _μ_
177
178LC_ALL=C $SH -c 'echo LC_ALL _?_'
179LC_ALL=C.UTF-8 $SH -c 'echo LC_ALL _?_'
180echo
181
182LC_CTYPE=C $SH -c 'echo LC_CTYPE _?_'
183LC_CTYPE=C.UTF-8 $SH -c 'echo LC_CTYPE _?_'
184echo
185
186LC_COLLATE=C $SH -c 'echo LC_COLLATE _?_'
187LC_COLLATE=C.UTF-8 $SH -c 'echo LC_COLLATE _?_'
188echo
189
190LANG=C $SH -c 'echo LANG _?_'
191LANG=C.UTF-8 $SH -c 'echo LANG _?_'
192
193## STDOUT:
194LC_ALL _x_
195LC_ALL _x_ _μ_
196
197LC_CTYPE _x_
198LC_CTYPE _x_ _μ_
199
200LC_COLLATE _x_
201LC_COLLATE _x_
202
203LANG _x_
204LANG _x_ _μ_
205## END
206
207## N-I dash/mksh STDOUT:
208LC_ALL _x_
209LC_ALL _x_
210
211LC_CTYPE _x_
212LC_CTYPE _x_
213
214LC_COLLATE _x_
215LC_COLLATE _x_
216
217LANG _x_
218LANG _x_
219## END
220
221
222#### LC_CTYPE=invalid
223
224# note: test/spec-common.sh sets LC_ALL
225unset LC_ALL
226
227touch _x_ _μ_
228
229{ LC_CTYPE=invalid $SH -c 'echo LC_CTYPE _?_'
230} 2> err.txt
231
232#cat err.txt
233wc -l err.txt
234
235## STDOUT:
236LC_CTYPE _x_
2371 err.txt
238## END
239
240## N-I dash/mksh/zsh STDOUT:
241LC_CTYPE _x_
2420 err.txt
243## END
244
245#### sh -c -- 'echo hi' does not run anything (#2637)
246
247$SH -c -z 'echo z'
248if test $? -ne 0; then
249 echo 'z failed'
250fi
251echo
252
253$SH -c --- 'echo three'
254if test $? -ne 0; then
255 echo three failed
256fi
257echo
258
259$SH -c -- 'echo two'
260echo two=$?
261echo
262
263$SH -c - 'echo one'
264echo one=$?
265echo
266
267$SH -c '' 'echo zero'
268echo zero=$?
269echo
270
271# odd
272$SH -c 'echo aa' 'echo bb'
273echo aa=$?
274
275## STDOUT:
276z failed
277
278three failed
279
280two
281two=0
282
283one
284one=0
285
286zero=0
287
288aa
289aa=0
290## END
291
292## BUG ash STDOUT:
293z failed
294
295three
296
297two
298two=0
299
300one
301one=0
302
303zero=0
304
305aa
306aa=0
307## END
308
309#### sh -c with multiple -- args
310
311# variant of above case
312
313$SH -c -- -- 'echo two'
314echo status=$?
315
316$SH -c -- -- -- 'echo two'
317echo status=$?
318
319$SH -c -z 'echo z'
320if test $? -ne 0; then
321 echo 'z failed'
322fi
323
324## STDOUT:
325status=127
326status=127
327z failed
328## END
329
330#### sh -c with no arg after --
331
332# variant of above case
333
334$SH -c --
335if test $? -ne 0; then
336 echo failed
337fi
338
339$SH -c -
340if test $? -ne 0; then
341 echo failed
342fi
343
344## STDOUT:
345failed
346failed
347## END
348
349#### Other flag parsers are not affected by - rule
350
351# -c is special, with quit_parsing_flags
352
353echo 'foo-bar' | { read -d -; echo reply=$REPLY; }
354
355## STDOUT:
356reply=foo
357## END
358## N-I dash STDOUT:
359reply=
360## END
361
362#### weird flag parsing -oo errexit noglob
363
364prog='
365case $- in
366 *e*) echo e ;;
367esac
368
369case $- in
370 *f*) echo f ;;
371esac
372'
373
374# normal way
375$SH -o errexit -o noglob -c "$prog"
376
377# odd way
378$SH -oo errexit noglob -c "$prog"
379
380## STDOUT:
381e
382f
383e
384f
385## END
386
387## BUG mksh status: 1
388## BUG mksh STDOUT:
389e
390f
391## END
392
393## BUG-2 zsh status: 1
394## BUG-2 zsh STDOUT:
395e
396## END
397
398#### 'sh -c -z' does not try to run -z
399
400$SH -c -z
401case $? in
402 1) echo flag-parsing-error ;;
403 2) echo flag-parsing-error ;;
404 *) echo fail ;;
405esac
406
407$SH -c 'echo 0=$0 1=$1' -z
408echo status=$?
409
410$SH -c 'echo 0=$0 1=$1' foo -z
411echo status=$?
412
413## STDOUT:
414flag-parsing-error
4150=-z 1=
416status=0
4170=foo 1=-z
418status=0
419## END
420
421#### sh -c -x 'echo hi' - order is reversed
422case $SH in zsh) exit ;; esac # different -x format
423
424$SH -c -x 'echo hi'
425
426# two flags before the command
427$SH -c -x -e 'zz; true' 2> /dev/null
428echo status=$?
429
430## STDOUT:
431hi
432status=127
433## END
434## STDERR:
435+ echo hi
436## END
437
438## OK zsh STDOUT:
439## END
440## OK zsh STDERR:
441## END