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

243 lines, 105 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