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

161 lines, 134 significant
1## compare_shells: dash bash mksh
2
3# Cases from
4# http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
5
6# My tests
7
8#### Empty for loop is allowed
9set -- a b
10for x in; do
11 echo hi
12 echo $x
13done
14## stdout-json: ""
15
16#### Empty for loop without in. Do can be on the same line I guess.
17set -- a b
18for x do
19 echo hi
20 echo $x
21done
22## STDOUT:
23hi
24a
25hi
26b
27## END
28
29#### Empty case statement
30case foo in
31esac
32## stdout-json: ""
33
34#### Last case without ;;
35foo=a
36case $foo in
37 a) echo A ;;
38 b) echo B
39esac
40## stdout: A
41
42#### Only case without ;;
43foo=a
44case $foo in
45 a) echo A
46esac
47## stdout: A
48
49#### Case with optional (
50foo=a
51case $foo in
52 (a) echo A ;;
53 (b) echo B
54esac
55## stdout: A
56
57#### Empty action for case is syntax error
58# POSIX grammar seems to allow this, but bash and dash don't. Need ;;
59foo=a
60case $foo in
61 a)
62 b)
63 echo A ;;
64 d)
65esac
66## status: 2
67## OK mksh status: 1
68
69#### Empty action is allowed for last case
70foo=b
71case $foo in
72 a) echo A ;;
73 b)
74esac
75## stdout-json: ""
76
77#### Case with | pattern
78foo=a
79case $foo in
80 a|b) echo A ;;
81 c)
82esac
83## stdout: A
84
85
86#### Bare semi-colon not allowed
87# This is disallowed by the grammar; bash and dash don't accept it.
88;
89## status: 2
90## OK mksh status: 1
91
92
93
94#
95# Explicit tests
96#
97
98
99
100#### Command substitution in default
101echo ${x:-$(ls -d /bin)}
102## stdout: /bin
103
104
105#### Arithmetic expansion
106x=3
107while [ $x -gt 0 ]
108do
109 echo $x
110 x=$(($x-1))
111done
112## STDOUT:
1133
1142
1151
116## END
117
118#### Newlines in compound lists
119x=3
120while
121 # a couple of <newline>s
122
123 # a list
124 date && ls -d /bin || echo failed; cat tests/hello.txt
125 # a couple of <newline>s
126
127 # another list
128 wc tests/hello.txt > _tmp/posix-compound.txt & true
129
130do
131 # 2 lists
132 ls -d /bin
133 cat tests/hello.txt
134 x=$(($x-1))
135 [ $x -eq 0 ] && break
136done
137# Not testing anything but the status since output is complicated
138## status: 0
139
140#### Multiple here docs on one line
141cat <<EOF1; cat <<EOF2
142one
143EOF1
144two
145EOF2
146## STDOUT:
147one
148two
149## END
150
151#### cat here doc; echo; cat here doc
152cat <<EOF1; echo two; cat <<EOF2
153one
154EOF1
155three
156EOF2
157## STDOUT:
158one
159two
160three
161## END