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