From 8aacb14e9d0a87828652f37778f0d838998d3b53 Mon Sep 17 00:00:00 2001 From: Manuel Vergara Date: Wed, 9 Jul 2025 10:08:33 +0200 Subject: [PATCH] Add new script --- 01_bash/calculate_workhours_pc.sh | 32 +++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 01_bash/calculate_workhours_pc.sh diff --git a/01_bash/calculate_workhours_pc.sh b/01_bash/calculate_workhours_pc.sh new file mode 100644 index 0000000..7a53492 --- /dev/null +++ b/01_bash/calculate_workhours_pc.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# Cabecera +printf "%-25s %-25s %-20s\n" "Encendido" "Apagado" "Tiempo activo" +printf "%-25s %-25s %-20s\n" "----------" "--------" "--------------" + +# Usamos `tac` para invertir el orden de `last -x` +last -x | tac | awk ' +/reboot/ { + boot = $6" "$7" "$8 + next +} +/shutdown/ && boot != "" { + shut = $6" "$7" "$8 + cmd1 = "date -d \""boot"\" +%s"; cmd1 | getline t1; close(cmd1); + cmd2 = "date -d \""shut"\" +%s"; cmd2 | getline t2; close(cmd2); + delta = t2 - t1 + if (delta > 0) { + h = int(delta / 3600) + m = int((delta % 3600) / 60) + s = delta % 60 + printf "%-25s %-25s %02d:%02d:%02d\n", boot, shut, h, m, s + total += delta + } + boot = "" +} +END { + h = int(total / 3600) + m = int((total % 3600) / 60) + s = total % 60 + printf "\n%-51s %02d:%02d:%02d\n", "Tiempo total encendido:", h, m, s +}'