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 line (not "s")
31 pp line (not 3)
32 pp line (not 4.5)
33 pp line (not {})
34 pp line (not [])
35 pp line (not false)
36 pp line (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
66 python2 -c 'print(None or "s")'
67 python2 -c 'print(None and "s")'
68
69 python2 -c 'print("x" or "y")'
70 python2 -c 'print("x" and "y")'
71
72 python2 -c 'print("" or "y")'
73 python2 -c 'print("" and "y")'
74
75 python2 -c 'print(42 or 0)'
76 python2 -c 'print(42 and 0)'
77
78 python2 -c 'print(0 or 42)'
79 python2 -c 'print(0 and 42)'
80
81 python2 -c 'print(0.0 or 0.5)'
82 python2 -c 'print(0.0 and 0.5)'
83
84 python2 -c 'print(["a"] or [])'
85 python2 -c 'print(["a"] and [])'
86
87 python2 -c 'print({"d": 1} or {})'
88 python2 -c 'print({"d": 1} and {})'
89
90 python2 -c 'print(0 or 0.0 or False or [] or {} or "OR")'
91 python2 -c 'print(1 and 1.0 and True and [5] and {"d":1} and "AND")'
92
93 echo ---
94
95 json write (null or "s")
96 json write (null and "s")
97
98 echo $["x" or "y"]
99 echo $["x" and "y"]
100
101 echo $["" or "y"]
102 echo $["" and "y"]
103
104 echo $[42 or 0]
105 echo $[42 and 0]
106
107 echo $[0 or 42]
108 echo $[0 and 42]
109
110 echo $[0.0 or 0.5]
111 echo $[0.0 and 0.5]
112
113 pp line (["a"] or [])
114 pp line (["a"] and [])
115
116 pp line ({"d": 1} or {})
117 pp line ({"d": 1} and {})
118
119 echo $[0 or 0.0 or false or [] or {} or "OR"]
120 echo $[1 and 1.0 and true and [5] and {"d":1} and "AND"]
121
122 declare -a array=(1 2 3)
123 pp line (array or 'yy')
124
125 declare -A assoc=([k]=v)
126 pp line (assoc or 'zz')
127
128 ## STDOUT:
129 s
130 None
131 x
132 y
133 y
134
135 42
136 0
137 42
138 0
139 0.5
140 0.0
141 ['a']
142 []
143 {'d': 1}
144 {}
145 OR
146 AND
147 ---
148 "s"
149 null
150 x
151 y
152 y
153
154 42
155 0
156 42
157 0
158 0.5
159 0.0
160 (List) ["a"]
161 (List) []
162 (Dict) {"d":1}
163 (Dict) {}
164 OR
165 AND
166 (BashArray) ["1","2","3"]
167 (BashAssoc) {"k":"v"}
168 ## END
169
170 #### x if b else y
171 var b = true
172 var i = 42
173 var t = i+1 if b else i-1
174 echo $t
175 var f = i+1 if false else i-1
176 echo $f
177 ## STDOUT:
178 43
179 41
180 ## END
181