So, if anyone wants it, I threw together this simple script to show battery status including if it's charging or discharging, charge / discharge rate, current wear level, and time left to charge or discharge at current rate.
Code:#!/bin/bash bats=`ls /proc/acpi/battery/`; for bat in $bats;do remain=`cat /proc/acpi/battery/$bat/{info,state}|grep remaining|grep -o "[0-9]\+"` full=`cat /proc/acpi/battery/$bat/{info,state}|grep full |grep -o "[0-9]\+"` descap=`cat /proc/acpi/battery/$bat/{info,state}|grep "design capacity:" |grep -o "[0-9]\+"` rate=`cat /proc/acpi/battery/$bat/{info,state}|grep "present rate:" |grep -o "[0-9]\+"` state=`cat /proc/acpi/battery/$bat/{info,state}|grep "charging state:" |grep -o "[a-z]\+$"` echo $state; lostcap=$((((descap-full)*100)/descap)); echo "Lost Capacity:" $lostcap"%" percfull=$(((remain)*100/full)); echo "Percent Full: " $percfull"%" if [[ "$rate" -ne 0 ]]; then if [[ "$state" = 'discharging' || "$state" = 'charged' ]]; then timeleft=$((((remain)*60)/rate)) echo "Time left to run: "$timeleft" minutes" else timeleft=$((((full-remain)*60)/rate)) echo "Time left to full: "$timeleft" minutes" fi fi; done
-
Nice cool script
Just one suggestion, you can store the output of 'cat /proc/acpi/battery/$bat/{info,state}' in a variable instead of reading it every time and echo that variable and pipe it to grep.
eg.
-
-
What kernel do you use?
/proc/acpi/battery is considered deprecated since somewhere between 2.6.18 and 2.6.26 in the vanilla kernel.
The new location (with changed structure) is:
/sys/class/power_supply/battery -
More info:
Linux kernel configuration help for CONFIG_ACPI_PROCFS_POWER in 2.6.34 on x86
So you could modify your script such that if it finds /sys interface first, use that otherwise stick with /proc.
Also, you can add one more check where the acpi directory exists but battery and ac_adapter directories wont exist (in case of desktops) - and print a message "No battery found" or something similar... -
Hey! Patches people! haha, just kidding, I can look at changing it. I'm on Ubuntu 10.10 and it doesn't have a /sys/class/power_supply/battery directory on it, so I'm not familiar with the layout of that if it's any different. But it's easy enough to make the changes. also, I know for a fact some parts of this will break on my Acer AO722 which doesn't give current charge rate when it's charging. So no estimating time til charged.
battinfo a script to show battery info and wear from the command line
Discussion in 'Linux Compatibility and Software' started by Sxooter, Aug 24, 2011.