1 | ## our_shell: ysh
|
2 | ## oils_failures_allowed: 0
|
3 |
|
4 | #### For loop over expression: List
|
5 | var mylist = [1, 2, 3]
|
6 | for item in (mylist) {
|
7 | echo "item $item"
|
8 | }
|
9 |
|
10 | ## STDOUT:
|
11 | item 1
|
12 | item 2
|
13 | item 3
|
14 | ## END
|
15 |
|
16 | #### For loop over expression: Dict, not BashAssoc
|
17 |
|
18 | var mydict = {name: 'bob', age: 40}
|
19 | for key in (mydict) {
|
20 | echo "key $key"
|
21 | }
|
22 |
|
23 | declare -A A=([name]=bob)
|
24 | for key in (A) {
|
25 | echo "key $key"
|
26 | }
|
27 |
|
28 | ## status: 3
|
29 | ## STDOUT:
|
30 | key name
|
31 | key age
|
32 | ## END
|
33 |
|
34 |
|
35 | #### For loop over range
|
36 | var myrange = 0 .. 3
|
37 | for i in (myrange) {
|
38 | echo "i $i"
|
39 | }
|
40 |
|
41 | ## STDOUT:
|
42 | i 0
|
43 | i 1
|
44 | i 2
|
45 | ## END
|
46 |
|
47 | #### Shell for loop with index (equivalent of enumerate())
|
48 | for i, item in a b c {
|
49 | echo "$i $item"
|
50 | }
|
51 | ## STDOUT:
|
52 | 0 a
|
53 | 1 b
|
54 | 2 c
|
55 | ## END
|
56 |
|
57 | #### 3 indices with (mylist) is a runtime error
|
58 | for i, item bad in (['foo', 'bar']) {
|
59 | echo "$i $item $bad"
|
60 | }
|
61 | ## status: 2
|
62 |
|
63 | #### Shell for loop can't have 3 indices
|
64 | for i, bad, bad in a b c {
|
65 | echo $i $item
|
66 | }
|
67 | ## status: 2
|
68 |
|
69 | #### Any for loop can't have 4 indiecs
|
70 | for a, b, c, d in (['foo']) {
|
71 | echo $i $item
|
72 | }
|
73 | ## status: 2
|
74 |
|
75 | #### Expression for loop with index: List
|
76 | for i, item in (['spam', 'eggs']) {
|
77 | echo "$i $item"
|
78 | }
|
79 | ## STDOUT:
|
80 | 0 spam
|
81 | 1 eggs
|
82 | ## END
|
83 |
|
84 | #### Expression for loop with index: Dict (TODO: define dict iter order)
|
85 | for key, value in ({name: 'bob', age: 40}) {
|
86 | echo "pair $key $value"
|
87 | }
|
88 | ## STDOUT:
|
89 | pair name bob
|
90 | pair age 40
|
91 | ## END
|
92 |
|
93 | #### Dict: index key value loop (TODO: define dict iter order)
|
94 | for i, key, value in ({name: 'bob', age: 40}) {
|
95 | echo "entry $i $key $value"
|
96 | }
|
97 | ## STDOUT:
|
98 | entry 0 name bob
|
99 | entry 1 age 40
|
100 | ## END
|
101 |
|
102 |
|
103 | #### Equivalent of zip()
|
104 |
|
105 | var array = :| d e f |
|
106 |
|
107 | for i, item in a b c {
|
108 | echo "$i $item $[array[i]]"
|
109 | }
|
110 |
|
111 | ## STDOUT:
|
112 | 0 a d
|
113 | 1 b e
|
114 | 2 c f
|
115 | ## END
|
116 |
|
117 | #### parse_bare_word eliminates confusion
|
118 |
|
119 | shopt --unset parse_bare_word
|
120 |
|
121 | for x in mylist { # THIS FAILS
|
122 | echo "BAD $x"
|
123 | }
|
124 |
|
125 | ## status: 2
|
126 | ## STDOUT:
|
127 | ## END
|
128 |
|
129 |
|
130 | #### Object that's not iterable
|
131 |
|
132 | echo hi
|
133 | for x in (42) {
|
134 | echo $x
|
135 | }
|
136 |
|
137 | ## status: 3
|
138 | ## STDOUT:
|
139 | hi
|
140 | ## END
|
141 |
|
142 | #### YSH for with brace substitution and glob
|
143 |
|
144 | touch {foo,bar}.py
|
145 | for i, file in *.py {README,foo}.md {
|
146 | echo "$i $file"
|
147 | }
|
148 | ## STDOUT:
|
149 | 0 bar.py
|
150 | 1 foo.py
|
151 | 2 README.md
|
152 | 3 foo.md
|
153 | ## END
|
154 |
|
155 | #### for x in <> {
|
156 |
|
157 | # to avoid stdin conflict
|
158 |
|
159 | $SH $REPO_ROOT/spec/testdata/ysh-for-stdin.ysh
|
160 |
|
161 | ## STDOUT:
|
162 | -1-
|
163 | -2-
|
164 | -3-
|
165 |
|
166 | 0 1
|
167 | 1 2
|
168 | 2 3
|
169 |
|
170 | empty
|
171 | done
|
172 |
|
173 | empty2
|
174 | done2
|
175 |
|
176 | space
|
177 | hi
|
178 | ## END
|