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