OILS / spec / for-expr.test.sh View on Github | oilshell.org

117 lines, 72 significant
1#
2# Constructs borrowed from ksh. Hm I didn't realize zsh also implements these!
3# mksh implements most too.
4
5#### C-style for loop
6n=10
7for ((a=1; a <= n ; a++)) # Double parentheses, and naked 'n'
8do
9 if test $a = 3; then
10 continue
11 fi
12 if test $a = 6; then
13 break
14 fi
15 echo $a
16done
17## status: 0
18## STDOUT:
191
202
214
225
23## END
24
25#### For loop with and without semicolon
26for ((a=1; a <= 3; a++)); do
27 echo $a
28done
29for ((a=1; a <= 3; a++)) do
30 echo $a
31done
32## status: 0
33## STDOUT:
341
352
363
371
382
393
40## END
41
42#### Accepts { } syntax too
43for ((a=1; a <= 3; a++)) {
44 echo $a
45}
46## STDOUT:
471
482
493
50## END
51
52#### Empty init
53i=1
54for (( ;i < 4; i++ )); do
55 echo $i
56done
57## status: 0
58## STDOUT:
591
602
613
62## END
63
64#### Empty init and cond
65i=1
66for (( ; ; i++ )); do
67 if test $i = 4; then
68 break
69 fi
70 echo $i
71done
72## status: 0
73## STDOUT:
741
752
763
77## END
78
79#### Infinite loop with ((;;))
80a=1
81for (( ; ; )); do
82 if test $a = 4; then
83 break
84 fi
85 echo $((a++))
86done
87## status: 0
88## STDOUT:
891
902
913
92## END
93
94
95#### Arith lexer mode
96
97# bash is lenient; zsh disagrees
98
99for ((i = '3'; i < '5'; ++i)); do echo $i; done
100for ((i = "3"; i < "5"; ++i)); do echo $i; done
101for ((i = $'3'; i < $'5'; ++i)); do echo $i; done
102for ((i = $"3"; i < $"5"; ++i)); do echo $i; done
103
104## STDOUT:
1053
1064
1073
1084
1093
1104
1113
1124
113## END
114## OK zsh status: 1
115## OK zsh STDOUT:
116## END
117