1 |
#!/usr/bin/env bash |
2 |
|
3 |
#### implicit for loop |
4 |
# This is like "for i in $@". |
5 |
func() { |
6 |
for i; do |
7 |
echo $i |
8 |
done |
9 |
} |
10 |
func 1 2 3 |
11 |
## stdout-json: "1\n2\n3\n" |
12 |
|
13 |
#### empty for loop (has "in") |
14 |
set -- 1 2 3 |
15 |
for i in ; do |
16 |
echo $i |
17 |
done |
18 |
## stdout-json: "" |
19 |
|
20 |
#### for loop with invalid identifier |
21 |
# should be compile time error, but runtime error is OK too |
22 |
for - in a b c; do |
23 |
echo hi |
24 |
done |
25 |
## stdout-json: "" |
26 |
## status: 2 |
27 |
## OK bash/mksh status: 1 |
28 |
## BUG zsh stdout: hi |
29 |
## BUG zsh status: 1 |
30 |
|
31 |
#### Tilde expansion within for loop |
32 |
HOME=/home/bob |
33 |
for name in ~/src ~/git; do |
34 |
echo $name |
35 |
done |
36 |
## stdout-json: "/home/bob/src\n/home/bob/git\n" |
37 |
|
38 |
#### Brace Expansion within Array |
39 |
for i in -{a,b} {c,d}-; do |
40 |
echo $i |
41 |
done |
42 |
## stdout-json: "-a\n-b\nc-\nd-\n" |
43 |
## N-I dash stdout-json: "-{a,b}\n{c,d}-\n" |
44 |
|
45 |
#### using loop var outside loop |
46 |
func() { |
47 |
for i in a b c; do |
48 |
echo $i |
49 |
done |
50 |
echo $i |
51 |
} |
52 |
func |
53 |
## status: 0 |
54 |
## stdout-json: "a\nb\nc\nc\n" |
55 |
|
56 |
#### continue |
57 |
for i in a b c; do |
58 |
echo $i |
59 |
if test $i = b; then |
60 |
continue |
61 |
fi |
62 |
echo $i |
63 |
done |
64 |
## status: 0 |
65 |
## stdout-json: "a\na\nb\nc\nc\n" |
66 |
|
67 |
#### break |
68 |
for i in a b c; do |
69 |
echo $i |
70 |
if test $i = b; then |
71 |
break |
72 |
fi |
73 |
done |
74 |
## status: 0 |
75 |
## stdout-json: "a\nb\n" |
76 |
|
77 |
#### while in while condition |
78 |
# This is a consequence of the grammar |
79 |
while while true; do echo cond; break; done |
80 |
do |
81 |
echo body |
82 |
break |
83 |
done |
84 |
## stdout-json: "cond\nbody\n" |
85 |
|
86 |
#### while in pipe |
87 |
x=$(find spec/ | wc -l) |
88 |
y=$(find spec/ | while read path; do |
89 |
echo $path |
90 |
done | wc -l |
91 |
) |
92 |
test $x -eq $y |
93 |
echo status=$? |
94 |
## stdout: status=0 |
95 |
|
96 |
#### while in pipe with subshell |
97 |
i=0 |
98 |
find . -maxdepth 1 -name INSTALL.txt -o -name LICENSE.txt | ( while read path; do |
99 |
i=$((i+1)) |
100 |
#echo $i |
101 |
done |
102 |
echo $i ) |
103 |
## stdout: 2 |
104 |
|
105 |
#### until loop |
106 |
# This is just the opposite of while? while ! cond? |
107 |
until false; do |
108 |
echo hi |
109 |
break |
110 |
done |
111 |
## stdout: hi |
112 |
|
113 |
#### continue at top level |
114 |
# zsh behaves with strict-control-flow! |
115 |
if true; then |
116 |
echo one |
117 |
continue |
118 |
echo two |
119 |
fi |
120 |
## status: 0 |
121 |
## STDOUT: |
122 |
one |
123 |
two |
124 |
## END |
125 |
## OK zsh status: 1 |
126 |
## OK zsh STDOUT: |
127 |
one |
128 |
## END |
129 |
|
130 |
#### continue in subshell |
131 |
for i in $(seq 3); do |
132 |
echo "> $i" |
133 |
( if true; then continue; fi; echo "Should not print" ) |
134 |
echo ". $i" |
135 |
done |
136 |
## STDOUT: |
137 |
> 1 |
138 |
. 1 |
139 |
> 2 |
140 |
. 2 |
141 |
> 3 |
142 |
. 3 |
143 |
## END |
144 |
## BUG mksh STDOUT: |
145 |
> 1 |
146 |
Should not print |
147 |
. 1 |
148 |
> 2 |
149 |
Should not print |
150 |
. 2 |
151 |
> 3 |
152 |
Should not print |
153 |
. 3 |
154 |
## END |