Skip to content

Instantly share code, notes, and snippets.

@djgraham
Created December 5, 2011 13:45
Show Gist options
  • Save djgraham/1433626 to your computer and use it in GitHub Desktop.
Save djgraham/1433626 to your computer and use it in GitHub Desktop.
Bash script for linux to show you how worn your battery is
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment