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