Commit | Line | Data |
---|---|---|
f37162a4 SK |
1 | #! /usr/bin/awk -f |
2 | ||
688fb20e | 3 | /^OK/ { debug("OK line", $0) } |
e16fe0ea | 4 | |
688fb20e SK |
5 | /^ERROR/ { |
6 | debug("ERROR line", $0) | |
7 | shift() | |
8 | msg_head = $1 | |
9 | shift() | |
10 | msg_body = $0 | |
11 | alert_trigger_hi(msg_head, "KhatusSensorError", msg_body) | |
12 | } | |
13 | ||
14 | /^OK in:ENERGY battery/\ | |
f37162a4 | 15 | { |
ab99b556 | 16 | debug("ENERGY battery", $0) |
688fb20e | 17 | sub("%$", "", $5) |
7851644f | 18 | db["energy_state_prev"] = db["energy_state_curr"] |
688fb20e SK |
19 | db["energy_state_curr"] = $4 |
20 | db["energy_percentage"] = ensure_numeric($5) | |
f03e88c6 | 21 | alert_check_energy_battery() |
ab99b556 SK |
22 | } |
23 | ||
688fb20e | 24 | /^OK in:ENERGY line_power/\ |
ab99b556 SK |
25 | { |
26 | debug("ENERGY line_power", $0) | |
27 | db["energy_line_power_prev"] = db["energy_line_power_curr"] | |
688fb20e | 28 | db["energy_line_power_curr"] = $4 |
f03e88c6 | 29 | alert_check_energy_line_power() |
f37162a4 SK |
30 | } |
31 | ||
688fb20e | 32 | /^OK in:MEMORY/\ |
f37162a4 | 33 | { |
688fb20e | 34 | shift() |
ab99b556 | 35 | shift() |
f37162a4 SK |
36 | db["memory_total"] = $1 |
37 | db["memory_used"] = $2 | |
38 | } | |
39 | ||
688fb20e | 40 | /^OK in:FAN +status:/\ |
f37162a4 | 41 | { |
688fb20e | 42 | shift() |
ab99b556 | 43 | shift() |
f37162a4 SK |
44 | db["fan_status"] = $2 |
45 | } | |
46 | ||
688fb20e | 47 | /^OK in:FAN +speed:/\ |
f37162a4 | 48 | { |
688fb20e | 49 | shift() |
ab99b556 | 50 | shift() |
f37162a4 SK |
51 | db["fan_speed"] = $2 |
52 | } | |
53 | ||
688fb20e | 54 | /^OK in:FAN +level:/\ |
f37162a4 | 55 | { |
688fb20e | 56 | shift() |
ab99b556 | 57 | shift() |
f37162a4 SK |
58 | db["fan_level"] = $2 |
59 | } | |
60 | ||
688fb20e | 61 | /^OK in:TEMPERATURE/\ |
f37162a4 | 62 | { |
688fb20e | 63 | shift() |
ab99b556 | 64 | shift() |
f37162a4 SK |
65 | db["temperature"] = $1 |
66 | } | |
67 | ||
688fb20e | 68 | /^OK in:LOAD_AVG/\ |
f37162a4 | 69 | { |
688fb20e | 70 | shift() |
ab99b556 | 71 | shift() |
f37162a4 SK |
72 | set_load_avg() |
73 | } | |
74 | ||
688fb20e | 75 | /^OK in:DISK_IO/\ |
f37162a4 | 76 | { |
688fb20e | 77 | shift() |
ab99b556 | 78 | shift() |
f37162a4 SK |
79 | set_disk_io() |
80 | } | |
81 | ||
688fb20e | 82 | /^OK in:DISK_SPACE/\ |
f37162a4 | 83 | { |
688fb20e | 84 | shift() |
ab99b556 | 85 | shift() |
9419890b | 86 | db["disk_space_used"] = $0 |
f37162a4 SK |
87 | } |
88 | ||
688fb20e | 89 | /^OK in:NET_ADDR_IO/\ |
f37162a4 | 90 | { |
688fb20e | 91 | shift() |
ab99b556 | 92 | shift() |
f37162a4 SK |
93 | set_net_addr_io() |
94 | } | |
95 | ||
688fb20e | 96 | /^OK in:NET_WIFI_STATUS/\ |
f37162a4 | 97 | { |
688fb20e | 98 | shift() |
ab99b556 | 99 | shift() |
b4612a8a | 100 | set_net_wifi_status() |
f37162a4 SK |
101 | } |
102 | ||
688fb20e | 103 | /^OK in:BLUETOOTH_POWER/\ |
f37162a4 | 104 | { |
688fb20e | 105 | shift() |
ab99b556 | 106 | shift() |
9419890b | 107 | db["bluetooth_power"] = $0 |
f37162a4 SK |
108 | } |
109 | ||
688fb20e | 110 | /^OK in:SCREEN_BRIGHTNESS/\ |
f37162a4 | 111 | { |
688fb20e | 112 | shift() |
ab99b556 | 113 | shift() |
f37162a4 SK |
114 | set_screen_brightness() |
115 | } | |
116 | ||
688fb20e | 117 | /^OK in:VOLUME/\ |
f37162a4 | 118 | { |
688fb20e | 119 | shift() |
d06e7260 | 120 | set_volume() |
f37162a4 SK |
121 | } |
122 | ||
688fb20e SK |
123 | /^OK in:MPD_SONG OK +MPD/ { delete db_mpd_song; next } |
124 | /^OK in:MPD_SONG OK$/ { set_mpd_playing() ; next } | |
125 | /^OK in:MPD_SONG / { set_mpd_song() ; next } | |
f5dfebd2 | 126 | |
688fb20e | 127 | /^OK in:MPD_STATE /\ |
a3bb1cff | 128 | { |
688fb20e | 129 | shift() |
ab99b556 | 130 | shift() |
a3bb1cff SK |
131 | db["mpd_status_state"] = $1 |
132 | db["mpd_status_time"] = $2 | |
133 | db["mpd_status_percent"] = $3 | |
f37162a4 SK |
134 | } |
135 | ||
688fb20e | 136 | /^OK in:WEATHER/\ |
f37162a4 | 137 | { |
688fb20e | 138 | shift() |
ab99b556 | 139 | shift() |
9419890b | 140 | db["weather_temperature"] = $0 |
f37162a4 SK |
141 | } |
142 | ||
688fb20e | 143 | /^OK in:DATE_TIME/\ |
f37162a4 | 144 | { |
688fb20e | 145 | shift() |
ab99b556 | 146 | shift() |
9419890b | 147 | db["datetime"] = $0 |
cae87802 SK |
148 | output_msg_status_bar(make_status_bar()) |
149 | } | |
150 | ||
d06e7260 SK |
151 | function set_volume( mute, left, right) { |
152 | # 0 RUNNING no 75% 75% | |
153 | #msg_head = $1 | |
154 | #sink = $2 | |
155 | #state = $3 | |
156 | mute = $4 | |
157 | left = $5 | |
158 | right = $6 | |
159 | ||
160 | if (mute == "no") { | |
161 | db["volume"] = sprintf("%s %s", left, right) | |
162 | } else if (mute == "yes") { | |
163 | db["volume"] = "X" | |
164 | } else { | |
0684c786 | 165 | error("set_volume", "Unexpected value for 'mute' field: " mute) |
d06e7260 SK |
166 | } |
167 | } | |
168 | ||
30b9a4e1 | 169 | function set_mpd_song( key, val) { |
688fb20e | 170 | shift() |
30b9a4e1 | 171 | key = $2 |
ab99b556 SK |
172 | shift() |
173 | shift() | |
30b9a4e1 SK |
174 | val = $0 |
175 | db_mpd_song[key] = val | |
bb1e4fee | 176 | debug("set_mpd_song", "", db_mpd_song) |
30b9a4e1 SK |
177 | } |
178 | ||
f5dfebd2 SK |
179 | function set_mpd_playing( \ |
180 | currently_playing, name, title, file, last, parts\ | |
181 | ) { | |
bb1e4fee | 182 | debug("set_mpd_playing", "", db_mpd_song) |
30b9a4e1 SK |
183 | name = db_mpd_song["Name:"] |
184 | title = db_mpd_song["Title:"] | |
185 | file = db_mpd_song["file:"] | |
f5dfebd2 SK |
186 | |
187 | if (name) { | |
188 | currently_playing = name | |
189 | } else if (title) { | |
190 | currently_playing = title | |
191 | } else if (file) { | |
192 | last = split(file, parts, "/") | |
193 | currently_playing = parts[last] | |
194 | } else { | |
195 | currently_playing = "" | |
196 | } | |
197 | db["mpd_playing_prev"] = db["mpd_playing_curr"] | |
198 | db["mpd_playing_curr"] = currently_playing | |
199 | ||
200 | alert_check_mpd() | |
201 | } | |
202 | ||
30b9a4e1 | 203 | function alert_check_mpd( curr, prev, name, body) { |
f5dfebd2 SK |
204 | prev = db["mpd_playing_prev"] |
205 | curr = db["mpd_playing_curr"] | |
e8c1404e | 206 | if (curr && curr != prev) { |
30b9a4e1 SK |
207 | name = db_mpd_song["Name:"] |
208 | if (name) { | |
209 | body = name | |
210 | } else { | |
211 | body = \ | |
212 | db_mpd_song["Artist:"] \ | |
213 | " - " db_mpd_song["Album:"] \ | |
214 | " - " db_mpd_song["Title:"] | |
215 | } | |
43e49903 | 216 | alert_trigger_low("alert_check_mpd", "MpdNowPlaying", body) |
f5dfebd2 SK |
217 | } |
218 | } | |
219 | ||
cae87802 SK |
220 | # TODO: Generalize alert spec lang |
221 | # - trigger threshold | |
222 | # - above/bellow/equal to threshold value | |
223 | # - priority | |
224 | # - snooze time (if already alerted, when to re-alert?) | |
225 | # - text: subject/body | |
f03e88c6 SK |
226 | function alert_check_energy_battery( \ |
227 | from, dbg, state_curr, state_prev, remaining, subj, body\ | |
7851644f | 228 | ) { |
f03e88c6 | 229 | from = "alert_check_energy_battery" |
d4c26c51 | 230 | |
7851644f SK |
231 | state_curr = db["energy_state_curr"] |
232 | state_prev = db["energy_state_prev"] | |
233 | remaining = db["energy_percentage"] | |
e16fe0ea | 234 | |
7851644f | 235 | dbg["state_curr"] = state_curr |
e16fe0ea | 236 | dbg["remaining"] = remaining |
bb1e4fee | 237 | debug(from, "", dbg) |
e16fe0ea | 238 | |
ab99b556 | 239 | if (state_curr == "discharging") { |
cae87802 SK |
240 | if (remaining < 5) { |
241 | subj = "Energy_CRITICALLY_Low" | |
242 | body = sprintf("%d%% CHARGE NOW!!! GO GO GO!!!", remaining) | |
d4c26c51 | 243 | alert_trigger_hi(from, subj, body) |
cae87802 SK |
244 | } else if (remaining < 10) { |
245 | subj = "Energy_Very_Low" | |
246 | body = sprintf("%d%% Plug it in ASAP.", remaining) | |
d4c26c51 | 247 | alert_trigger_hi(from, subj, body) |
cae87802 SK |
248 | } else if (remaining < 15) { |
249 | subj = "Energy_Low" | |
250 | body = sprintf("%d%% Get the charger.", remaining) | |
d4c26c51 | 251 | alert_trigger_hi(from, subj, body) |
361523eb SK |
252 | } else if (remaining < 20) { |
253 | subj = "Energy_Low" | |
254 | body = sprintf("%d%% Get the charger.", remaining) | |
255 | alert_trigger_med(from, subj, body) | |
cae87802 SK |
256 | } else if (remaining < 50) { |
257 | if (!state__alerts__energy__notified_bellow_half) { | |
258 | state__alerts__energy__notified_bellow_half = 1 | |
259 | subj = "Energy_Bellow_Half" | |
260 | body = sprintf("%d%% Where is the charger?", remaining) | |
d4c26c51 | 261 | alert_trigger_med(from, subj, body) |
cae87802 SK |
262 | } |
263 | } | |
264 | } else { | |
265 | # TODO: Reconsider the competing global-state organizing-conventions | |
266 | state__alerts__energy__notified_bellow_half = 0 | |
267 | } | |
268 | } | |
269 | ||
f03e88c6 SK |
270 | function alert_check_energy_line_power( \ |
271 | from, dbg, line_power_curr, line_power_prev, subj, body \ | |
272 | ) { | |
273 | from = "alert_check_energy_line_power" | |
274 | ||
275 | dbg["energy_line_power_prev"] = db["energy_line_power_prev"] | |
276 | dbg["energy_line_power_curr"] = db["energy_line_power_curr"] | |
277 | debug(from, "", dbg) | |
278 | ||
279 | line_power_curr = db["energy_line_power_curr"] | |
280 | line_power_prev = db["energy_line_power_prev"] | |
281 | ||
282 | if (line_power_curr == "no" && line_power_prev != "no") { | |
283 | alert_trigger_low(from, "PowerUnplugged", "") | |
284 | } | |
285 | } | |
286 | ||
d4c26c51 SK |
287 | function alert_trigger_low(from, subject, body) { |
288 | alert_trigger("low", from, subject, body) | |
cae87802 SK |
289 | } |
290 | ||
d4c26c51 SK |
291 | function alert_trigger_med(from, subject, body) { |
292 | alert_trigger("med", from, subject, body) | |
cae87802 SK |
293 | } |
294 | ||
d4c26c51 SK |
295 | function alert_trigger_hi(from, subject, body) { |
296 | alert_trigger("hi", from, subject, body) | |
cae87802 SK |
297 | } |
298 | ||
d4c26c51 | 299 | function alert_trigger(priority, from, subject, body, msg) { |
cae87802 SK |
300 | # priority : "low" | "med" | "hi" |
301 | # subject : no spaces | |
302 | # body : anything | |
d4c26c51 | 303 | msg = sprintf("khatus_%s %s %s %s", from, priority, subject, body) |
cae87802 SK |
304 | output_msg_alert(msg) |
305 | } | |
306 | ||
307 | function output_msg_alert(msg) { | |
308 | # TODO: Should alerts go into a dedicated channel? | |
309 | output_msg("ALERT", msg, "/dev/stdout") | |
310 | } | |
311 | ||
312 | function output_msg_status_bar(msg) { | |
313 | output_msg("STATUS_BAR", msg, "/dev/stdout") | |
314 | } | |
315 | ||
316 | function output_msg(type, content, channel) { | |
317 | print(type, content) > channel | |
f37162a4 SK |
318 | } |
319 | ||
320 | function set_load_avg( sched) { | |
321 | split($4, sched, "/") | |
322 | db["load_avg_1min"] = $1 | |
323 | db["load_avg_5min"] = $2 | |
324 | db["load_avg_15min"] = $3 | |
325 | db["kern_sched_queue_runnable"] = sched[1] | |
326 | db["kern_sched_queue_total"] = sched[2] | |
327 | db["kern_sched_latest_pid"] = $5 | |
328 | } | |
329 | ||
330 | function set_disk_io( curr_w, curr_r, prev_w, prev_r) { | |
331 | curr_w = $1 | |
332 | curr_r = $2 | |
333 | prev_w = db["disk_io_curr_w"] | |
334 | prev_r = db["disk_io_curr_r"] | |
335 | db["disk_io_curr_w"] = curr_w | |
336 | db["disk_io_curr_r"] = curr_r | |
337 | db["disk_io_diff_w"] = curr_w - prev_w | |
338 | db["disk_io_diff_r"] = curr_r - prev_r | |
339 | } | |
340 | ||
b4612a8a SK |
341 | function set_net_wifi_status( interface) { |
342 | interface = $1 | |
343 | shift() | |
344 | db["net_wifi_status", interface] = $0 | |
345 | } | |
346 | ||
f37162a4 SK |
347 | function set_net_addr_io( \ |
348 | interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\ | |
349 | ) { | |
350 | interface = $1 | |
351 | address = $2 | |
352 | io_curr_w = $3 | |
353 | io_curr_r = $4 | |
354 | if (interface) { | |
355 | if (address && io_curr_w && io_curr_r) { | |
356 | # recalculate | |
357 | io_prev_w = net_io_curr_w[interface] | |
358 | io_prev_r = net_io_curr_r[interface] | |
359 | ||
360 | net_addr[interface] = address | |
361 | net_io_curr_w[interface] = io_curr_w | |
362 | net_io_curr_r[interface] = io_curr_r | |
363 | net_io_diff_w[interface] = io_curr_w - io_prev_w | |
364 | net_io_diff_r[interface] = io_curr_r - io_prev_r | |
365 | } else { | |
366 | # clear | |
367 | net_addr[interface] = "" | |
368 | net_io_curr_w[interface] = 0 | |
369 | net_io_curr_r[interface] = 0 | |
370 | net_io_diff_w[interface] = 0 | |
371 | net_io_diff_r[interface] = 0 | |
372 | } | |
373 | } | |
374 | } | |
375 | ||
376 | function set_screen_brightness( max, cur) { | |
377 | max = $1 | |
378 | cur = $2 | |
379 | db["screen_brightness"] = (cur / max) * 100 | |
380 | } | |
381 | ||
ab99b556 SK |
382 | # TODO: Revise overuse of shift() where it is not really needed |
383 | function shift() { | |
9419890b | 384 | sub("^" $1 " +", "") |
f37162a4 SK |
385 | } |
386 | ||
cae87802 | 387 | function make_status_bar( position, bar, sep, i, j) { |
f37162a4 SK |
388 | position[++i] = make_status_energy() |
389 | position[++i] = make_status_mem() | |
390 | position[++i] = make_status_cpu() | |
391 | position[++i] = make_status_disk() | |
392 | position[++i] = make_status_net() | |
393 | position[++i] = sprintf("B=%s", db["bluetooth_power"]) | |
394 | position[++i] = sprintf("*%d%%", db["screen_brightness"]) | |
395 | position[++i] = sprintf("(%s)", db["volume"]) | |
396 | position[++i] = make_status_mpd() | |
397 | position[++i] = db["weather_temperature"] | |
398 | position[++i] = db["datetime"] | |
399 | bar = "" | |
400 | sep = "" | |
401 | for (j = 1; j <= i; j++) { | |
402 | bar = bar sep position[j] | |
403 | sep = " " | |
404 | } | |
405 | return bar | |
406 | } | |
407 | ||
408 | function make_status_energy( state, direction_of_change) { | |
7851644f | 409 | state = db["energy_state_curr"] |
f37162a4 SK |
410 | if (state == "discharging") { |
411 | direction_of_change = "<" | |
412 | } else if (state == "charging") { | |
413 | direction_of_change = ">" | |
414 | } else { | |
415 | direction_of_change = "=" | |
416 | }; | |
e16fe0ea | 417 | return sprintf("E%s%d%%", direction_of_change, db["energy_percentage"]) |
f37162a4 SK |
418 | } |
419 | ||
420 | function make_status_mem( total, used, percent, status) { | |
421 | total = db["memory_total"] | |
422 | used = db["memory_used"] | |
423 | # To avoid division by zero when data is missing | |
424 | if (total && used) { | |
425 | percent = round((used / total) * 100) | |
426 | status = sprintf("%d%%", percent) | |
427 | } else { | |
428 | status = "__" | |
429 | } | |
430 | return sprintf("M=%s", status) | |
431 | } | |
432 | ||
433 | function make_status_cpu( load, temp, fan) { | |
434 | load = db["load_avg_1min"] | |
435 | temp = db["temperature"] / 1000 | |
436 | fan = db["fan_speed"] | |
437 | return sprintf("C=[%4.2f %d°C %4drpm]", load, temp, fan) | |
438 | } | |
439 | ||
440 | function make_status_disk( bytes_per_sector, bytes_per_mb, w, r) { | |
441 | bytes_per_sector = 512 | |
442 | bytes_per_mb = 1024 * 1024 | |
443 | w = (db["disk_io_diff_w"] * bytes_per_sector) / bytes_per_mb | |
444 | r = (db["disk_io_diff_r"] * bytes_per_sector) / bytes_per_mb | |
445 | return \ | |
446 | sprintf("D=[%s %0.3f▲ %0.3f▼]", db["disk_space_used"], w, r) | |
447 | } | |
448 | ||
449 | function make_status_net( \ | |
450 | out, | |
451 | number_of_interfaces_to_show, | |
452 | n, | |
453 | array_of_prefixes_of_interfaces_to_show, | |
454 | prefix, | |
455 | interface, | |
456 | label, | |
457 | count_printed, | |
458 | sep, | |
459 | io_stat, | |
460 | dw, dr, | |
461 | bytes_per_unit\ | |
462 | ) { | |
463 | out = "" | |
464 | number_of_interfaces_to_show = \ | |
465 | split(\ | |
466 | opt_prefixes_of_net_interfaces_to_show,\ | |
467 | array_of_prefixes_of_interfaces_to_show,\ | |
468 | ","\ | |
469 | ) | |
470 | for (n = 1; n <= number_of_interfaces_to_show; n++) { | |
471 | prefix = array_of_prefixes_of_interfaces_to_show[n] | |
472 | for (interface in net_addr) { | |
473 | if (interface ~ ("^" prefix)) { | |
474 | label = substr(interface, 1, 1) | |
475 | if (net_addr[interface]) { | |
476 | bytes_per_mb = 1024 * 1024 # TODO: option | |
477 | dw = net_io_diff_w[interface] / bytes_per_mb | |
478 | dr = net_io_diff_r[interface] / bytes_per_mb | |
479 | io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr) | |
480 | } else { | |
481 | io_stat = "--" | |
482 | } | |
483 | if (interface ~ "^w") { | |
b4612a8a | 484 | label = label ":" db["net_wifi_status", interface] |
f37162a4 SK |
485 | } |
486 | if (++count_printed > 1) { | |
487 | sep = " " | |
488 | } else { | |
489 | sep = "" | |
490 | } | |
491 | out = out sep label ":" io_stat | |
492 | } | |
493 | } | |
494 | } | |
495 | return sprintf("N[%s]", out) | |
496 | } | |
497 | ||
498 | function make_status_mpd( state, status) { | |
a3bb1cff | 499 | state = db["mpd_status_state"] |
f37162a4 SK |
500 | |
501 | if (state == "play") { | |
502 | status = make_status_mpd_state_known("▶") | |
503 | } else if (state == "pause") { | |
504 | status = make_status_mpd_state_known("❚❚") | |
505 | } else if (state == "stop") { | |
506 | status = make_status_mpd_state_known("⬛") | |
507 | } else { | |
508 | status = make_status_mpd_state_unknown("--") | |
509 | } | |
510 | ||
511 | return sprintf("[%s]", status) | |
512 | } | |
513 | ||
f5dfebd2 | 514 | function make_status_mpd_state_known(symbol) { |
f37162a4 SK |
515 | return sprintf(\ |
516 | "%s %s %s %s", | |
517 | symbol, | |
a3bb1cff SK |
518 | db["mpd_status_time"], |
519 | db["mpd_status_percent"], | |
f5dfebd2 | 520 | substr(db["mpd_playing_curr"], 1, opt_mpd_song_max_chars)\ |
f37162a4 SK |
521 | ) |
522 | } | |
523 | ||
524 | function make_status_mpd_state_unknown(symbol) { | |
525 | return sprintf("%s", symbol) | |
526 | } | |
527 | ||
528 | function round(n) { | |
529 | return int(n + 0.5) | |
530 | } | |
531 | ||
bb1e4fee | 532 | function debug(location, msg, values, sep, vals, key, payload) { |
f37162a4 | 533 | if (opt_debug) { |
e16fe0ea SK |
534 | sep = "" |
535 | vals = "" | |
536 | for (key in values) { | |
537 | vals = sprintf("%s%s%s: %s", vals, sep, key, values[key]) | |
538 | sep = ", " | |
539 | } | |
ab99b556 SK |
540 | payload = \ |
541 | sprintf("LOCATION[%s] MSG[%s] DATA[%s]", location, msg, vals) | |
bb1e4fee | 542 | output_msg("DEBUG", payload, "/dev/stderr") |
f37162a4 SK |
543 | } |
544 | } | |
e16fe0ea | 545 | |
0684c786 SK |
546 | function error(location, msg) { |
547 | # TODO: Reconsider classifying internal errors as alerts | |
548 | # Maybe better to keep the error class distinct and provide a | |
549 | # an optional transformation from error to alert | |
550 | alert_trigger_hi(location, "KhatusControllerError", msg) | |
7275500c SK |
551 | } |
552 | ||
e16fe0ea SK |
553 | function ensure_numeric(n) { |
554 | return n + 0 | |
555 | } | |
556 | #------------------------------- | |
557 | # Why do we need ensure_numeric? | |
558 | #------------------------------- | |
559 | # awk appears to be guessing the type of an inputted scalar based on usage, so | |
560 | # if we read-in a number, but did not use it in any numeric operations, but did | |
561 | # use as a string (even in just a format string!) - it will be treated as a | |
562 | # string and can lead to REALLY SURPRISING behavior in conditional statements, | |
563 | # where smaller number may compare as greater than the bigger ones, such as. | |
564 | # | |
565 | # Demo: | |
566 | # | |
567 | # $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %d, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' | |
568 | # 75 < 100 | |
569 | # $ awk 'BEGIN {x = "75"; y = "100"; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' | |
570 | # 75 > 100 | |
571 | ||
572 | # However, once used as a number, seems to stay that way even after being | |
573 | # used as string: | |
574 | # | |
575 | # $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' | |
576 | # 75 < 100 | |
577 | # | |
578 | # $ awk 'BEGIN {x = "75"; y = "100"; x + y; sprintf("x: %s, y: %d\n", x, y); z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' | |
579 | # 75 < 100 | |
580 | # | |
581 | # $ awk 'BEGIN {x = "75"; y = "100"; x + y; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' | |
582 | # 75 < 100 | |
583 | # $ awk 'BEGIN {x = "75"; y = "100"; z = x y; if (x > y) {print "75 > 100"} else if (x < y) {print "75 < 100"}}' | |
584 | # 75 > 100 |