1 #### or short circuits
2 shopt -s ysh:upgrade
3
4 var x = [1, 2]
5 if (true or x[3]) {
6 echo OK
7 }
8 ## STDOUT:
9 OK
10 ## END
11
12 #### and short circuits
13 shopt -s ysh:upgrade
14
15 var x = [1, 2]
16 if (false and x[3]) {
17 echo bad
18 } else {
19 echo OK
20 }
21
22 ## STDOUT:
23 OK
24 ## END
25
26 #### not operator behaves like Python
27
28 # consistent with if statement, ternary if, and, or
29
30 pp test_ (not "s")
31 pp test_ (not 3)
32 pp test_ (not 4.5)
33 pp test_ (not {})
34 pp test_ (not [])
35 pp test_ (not false)
36 pp test_ (not true)
37
38 ## STDOUT:
39 (Bool) false
40 (Bool) false
41 (Bool) false
42 (Bool) true
43 (Bool) true
44 (Bool) true
45 (Bool) false
46 ## END
47
48 #### not, and, or
49
50 var a = not true
51 echo $a
52 var b = true and false
53 echo $b
54 var c = true or false
55 echo $c
56
57 ## STDOUT:
58 false
59 false
60 true
61 ## END
62
63
64 #### and-or chains for typed data
65 shopt --set parse_ysh_expr_sub
66
67 python2 -c 'print(None or "s")'
68 python2 -c 'print(None and "s")'
69
70 python2 -c 'print("x" or "y")'
71 python2 -c 'print("x" and "y")'
72
73 python2 -c 'print("" or "y")'
74 python2 -c 'print("" and "y")'
75
76 python2 -c 'print(42 or 0)'
77 python2 -c 'print(42 and 0)'
78
79 python2 -c 'print(0 or 42)'
80 python2 -c 'print(0 and 42)'
81
82 python2 -c 'print(0.0 or 0.5)'
83 python2 -c 'print(0.0 and 0.5)'
84
85 python2 -c 'print(["a"] or [])'
86 python2 -c 'print(["a"] and [])'
87
88 python2 -c 'print({"d": 1} or {})'
89 python2 -c 'print({"d": 1} and {})'
90
91 python2 -c 'print(0 or 0.0 or False or [] or {} or "OR")'
92 python2 -c 'print(1 and 1.0 and True and [5] and {"d":1} and "AND")'
93
94 echo ---
95
96 json write (null or "s")
97 json write (null and "s")
98
99 echo $["x" or "y"]
100 echo $["x" and "y"]
101
102 echo $["" or "y"]
103 echo $["" and "y"]
104
105 echo $[42 or 0]
106 echo $[42 and 0]
107
108 echo $[0 or 42]
109 echo $[0 and 42]
110
111 echo $[0.0 or 0.5]
112 echo $[0.0 and 0.5]
113
114 pp test_ (["a"] or [])
115 pp test_ (["a"] and [])
116
117 pp test_ ({"d": 1} or {})
118 pp test_ ({"d": 1} and {})
119
120 echo $[0 or 0.0 or false or [] or {} or "OR"]
121 echo $[1 and 1.0 and true and [5] and {"d":1} and "AND"]
122
123 ## STDOUT:
124 s
125 None
126 x
127 y
128 y
129
130 42
131 0
132 42
133 0
134 0.5
135 0.0
136 ['a']
137 []
138 {'d': 1}
139 {}
140 OR
141 AND
142 ---
143 "s"
144 null
145 x
146 y
147 y
148
149 42
150 0
151 42
152 0
153 0.5
154 0.0
155 (List) ["a"]
156 (List) []
157 (Dict) {"d":1}
158 (Dict) {}
159 OR
160 AND
161 ## END
162
163 #### or BashArray, or BashAssoc
164 declare -a array=(1 2 3)
165 pp test_ (array or 'yy')
166
167 declare -A assoc=([k]=v)
168 pp test_ (assoc or 'zz')
169
170 ## STDOUT:
171 {"type":"BashArray","data":{"0":"1","1":"2","2":"3"}}
172 {"type":"BashAssoc","data":{"k":"v"}}
173 ## END
174
175 #### x if b else y
176 var b = true
177 var i = 42
178 var t = i+1 if b else i-1
179 echo $t
180 var f = i+1 if false else i-1
181 echo $f
182 ## STDOUT:
183 43
184 41
185 ## END
186