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 | ## STDOUT:
|
123 | s
|
124 | None
|
125 | x
|
126 | y
|
127 | y
|
128 |
|
129 | 42
|
130 | 0
|
131 | 42
|
132 | 0
|
133 | 0.5
|
134 | 0.0
|
135 | ['a']
|
136 | []
|
137 | {'d': 1}
|
138 | {}
|
139 | OR
|
140 | AND
|
141 | ---
|
142 | "s"
|
143 | null
|
144 | x
|
145 | y
|
146 | y
|
147 |
|
148 | 42
|
149 | 0
|
150 | 42
|
151 | 0
|
152 | 0.5
|
153 | 0.0
|
154 | (List) ["a"]
|
155 | (List) []
|
156 | (Dict) {"d":1}
|
157 | (Dict) {}
|
158 | OR
|
159 | AND
|
160 | ## END
|
161 |
|
162 | #### or BashArray, or BashAssoc
|
163 | declare -a array=(1 2 3)
|
164 | pp line (array or 'yy')
|
165 |
|
166 | declare -A assoc=([k]=v)
|
167 | pp line (assoc or 'zz')
|
168 |
|
169 | ## STDOUT:
|
170 | {"type":"BashArray","data":{"0":"1","1":"2","2":"3"}}
|
171 | {"type":"BashAssoc","data":{"k":"v"}}
|
172 | ## END
|
173 |
|
174 | #### x if b else y
|
175 | var b = true
|
176 | var i = 42
|
177 | var t = i+1 if b else i-1
|
178 | echo $t
|
179 | var f = i+1 if false else i-1
|
180 | echo $f
|
181 | ## STDOUT:
|
182 | 43
|
183 | 41
|
184 | ## END
|
185 |
|