From fcc67d6b83f7688fd6885b5543e705e6337fe08e Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Sat, 2 Sep 2023 16:09:43 -0600 Subject: [PATCH] Merge log battery stript --- systemd/system/timer-scripts/log_battery.sh | 36 +++++++++------------ 1 file changed, 16 insertions(+), 20 deletions(-) diff --git a/systemd/system/timer-scripts/log_battery.sh b/systemd/system/timer-scripts/log_battery.sh index d3f0102..3179181 100755 --- a/systemd/system/timer-scripts/log_battery.sh +++ b/systemd/system/timer-scripts/log_battery.sh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Look through `upower --dump` to set this variable +# Look through `upower --dump` to set the battery path variable declare -r BATTERY_PATH="$1" declare -r WRITE_PATH="$2" @@ -16,35 +16,31 @@ upower --dump > "$tmp_file" declare system_name="$(hostname)" declare current_time="$(date -u +"%Y-%m-%d %H:%M:%S UTC")" -declare current_power="$(cat "$tmp_file" | awk ' + +upower --dump | awk ' BEGIN { b = "'"$BATTERY_PATH"'"; t=0 } +t && (match($0, /Device:/) || match($0, /Daemon:/)) { + exit +} + match($0, b) { t=1 } t && match($0, /energy:/) { split($0,a," ") - print a[2] - exit -}')" -declare current_percentage="$(cat "$tmp_file" | awk ' -BEGIN { b = "'"$BATTERY_PATH"'"; t=0 } - -match($0, b) { t=1 } + energy = a[2] +} t && match($0, /percentage:/) { split($0,a," ") - print a[2] - exit -}')" -declare current_state="$(cat "$tmp_file" | awk ' -BEGIN { b = "'"$BATTERY_PATH"'"; t=0 } - -match($0, b) { t=1 } + percent = a[2] +} t && match($0, /state:/) { split($0,a," ") - print a[2] - exit -}')" + state = a[2] +} -echo "$system_name,$current_time,$current_power,$current_percentage,$current_state" >> "$WRITE_PATH" +END { + print "'"${system_name},${current_time},"'"energy","percent","state +}' >> "$WRITE_PATH"