POSIX-Complaint Battery-Management-Script
I use dwm as my window-manager and often forget to look at my bar to read the battery-percentage, so i wrote this. The script is designed as a Finite-State-Machine and depends on dunst and acpi. Feel free to reuse, change and critique.
#!/usr/bin/sh
STATE=0
STATE_PREV=0
while true; do
charge=$(acpi)
percentage=$(echo $$charge | awk '{print $4}' | sed -E 's/%,?//g' | sort -r | head -n 1)
STATE=0 # Cordless use; default
if $percentage -le 10; then
STATE=1 # Critical capacity
elif test "${charge#*Charging}"; then
STATE=2 # Charging
elif test "${charge#*Full}"; then
STATE=3 # Fully Charged
fi
if $STATE -eq 1 && $STATE_PREV -ne 1; then
dunstify --urgency=critical -t 0 'Battery' 'Battery less than 10%'
elif $STATE -eq 2 && $STATE_PREV -ne 2; then
dunstify -t 5 'Battery' 'Battery is charging'
elif $STATE -eq 3 && $STATE_PREV -ne 3; then
dunstify -t 5 'Battery' 'Battery fully charged!'
fi
sleep 40
STATE_PREV=$STATE
done
Edited 8 Oct 2019. We only hold this version.
About this post
- Posted
- Archived
- Edited
- Score
- 2 · 100% up
- Kind
- Text
- ID
- depssa
6 comments
It doesn't seem to handle the case of multiple batteries..! And sh does not understand [[... See https://shellcheck.net
Thanks for pointing me to shellcheck! I haven't heard of it before. Will use and improve the above tomorrow.
I updated the script. Should be POSIX-complaint now.
You're right about the multiple batteries. However i'm running the script with sh and it works.
Your
shis most likely bash anyway. Try and install busybox and use its shell to execute the script.As already said, I recommend you get shellcheck installed on your machine. I can see that there are several quotes missing at first sight.