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

118 lines, 72 significant
1## compare_shells: bash-4.4 zsh
2
3# Constructs borrowed from ksh. Hm I didn't realize zsh also implements these!
4# mksh implements most too.
5
6#### C-style for loop
7n=10
8for ((a=1; a <= n ; a++)) # Double parentheses, and naked 'n'
9do
10 if test $a = 3; then
11 continue
12 fi
13 if test $a = 6; then
14 break
15 fi
16 echo $a
17done
18## status: 0
19## STDOUT:
201
212
224
235
24## END
25
26#### For loop with and without semicolon
27for ((a=1; a <= 3; a++)); do
28 echo $a
29done
30for ((a=1; a <= 3; a++)) do
31 echo $a
32done
33## status: 0
34## STDOUT:
351
362
373
381
392
403
41## END
42
43#### Accepts { } syntax too
44for ((a=1; a <= 3; a++)) {
45 echo $a
46}
47## STDOUT:
481
492
503
51## END
52
53#### Empty init
54i=1
55for (( ;i < 4; i++ )); do
56 echo $i
57done
58## status: 0
59## STDOUT:
601
612
623
63## END
64
65#### Empty init and cond
66i=1
67for (( ; ; i++ )); do
68 if test $i = 4; then
69 break
70 fi
71 echo $i
72done
73## status: 0
74## STDOUT:
751
762
773
78## END
79
80#### Infinite loop with ((;;))
81a=1
82for (( ; ; )); do
83 if test $a = 4; then
84 break
85 fi
86 echo $((a++))
87done
88## status: 0
89## STDOUT:
901
912
923
93## END
94
95
96#### Arith lexer mode
97
98# bash is lenient; zsh disagrees
99
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
103for ((i = $"3"; i < $"5"; ++i)); do echo $i; done
104
105## STDOUT:
1063
1074
1083
1094
1103
1114
1123
1134
114## END
115## OK zsh status: 1
116## OK zsh STDOUT:
117## END
118