I added these two functions to my bashrc. The first one is used to get the battery charge, and the second returns the battery’s current capacity.
function bchrg {
# Returns battery charge as a percentage.
now=`cat /sys/class/power_supply/BAT0/energy_now`
full=`cat /sys/class/power_supply/BAT0/energy_full`
out=`echo $now/$full*100 | bc -l | cut -c 1-5`
echo "Charge: "$out"%"
}
function bcap {
# Returns battery capacity as a percentage.
design=`cat /sys/class/power_supply/BAT0/energy_full_design`
current=`cat /sys/class/power_supply/BAT0/energy_full`
out=`echo $current/$design*100 | bc -l | cut -c 1-5`
echo "Capacity: "$out"%"
}
0 Responses to “Bash Function to Get Battery Charge”