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

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