You are not logged in.

#1 2003-11-18 22:41:27

Guest
Guest

Simple script to get battery charge and time remaining...

Here's something I threw together last night, to report on the battery at the command line.  It uses /proc/acpi/battery.  My battery is C11F, just substitude your own.

It takes (remaining capacity) / (design capacity) to get the percent left, and it takes the (discharge rate) / (remaining capacity) to get approximate life.  If the rate is zero (which mine reports as when plugged in) it says the battery is charging.

I don't know if this will work for other people, or if different batteries give different times of information to ACPI.

#!/bin/bash

current=`grep remaining /proc/acpi/battery/C11F/state | tr -d A-z | tr -d " " | tr -d :`

total=`grep "design capacity:" /proc/acpi/battery/C11F/info | tr -d A-z | tr -d " " | tr -d :`

rate=`grep "present rate:" /proc/acpi/battery/C11F/state | tr -d A-z | tr -d " " | tr -d :`

percent=`bc << EOF
100.0 * $current / $total
EOF
`
if [ $rate -eq  0 ]; then
  echo $percent'%, charging'
else
hours=`bc << EOF
(1.0*$current) / (1.0*$rate) 
EOF
`
minutes=`bc << EOF
60.0 * $current / $rate - $hours * 60
EOF
`
echo $percent'%, '$hours':'$minutes
fi

#2 2003-11-18 23:06:30

zen_guerrilla
Member
From: Greece
Registered: 2002-12-22
Posts: 259

Re: Simple script to get battery charge and time remaining...

You can also try the 'acpi' pkg, which does exactly the same thing smile.

Offline

#3 2003-11-19 01:46:00

Guest
Guest

Re: Simple script to get battery charge and time remaining...

zen_guerrilla wrote:

You can also try the 'acpi' pkg, which does exactly the same thing smile.

Considering the acpi package is only dated a few days ago, I don't feel too bad about not knowing about it. big_smile

BTW, here's an update I did to my script.  This one prints minutes less than 10 correctly (1:02 vs 1:2), as well as finding batteries with other names, and printing the CPU speed (nice if you have speed stepping turned on).  It rounds to the nearest 100MHz, which is easy to remove if you don't like it.

#!/bin/bash
speed=`grep "cpu MHz" /proc/cpuinfo | tr -d A-z | tr -d " " | tr -d :`

current=`grep remaining /proc/acpi/battery/*/state | tr -d A-z | tr -d " " | tr -d :`

total=`grep "design capacity:" /proc/acpi/battery/*/info | tr -d A-z | tr -d " " | tr -d :`

rate=`grep "present rate:" /proc/acpi/battery/*/state | tr -d A-z | tr -d " " | tr -d :`

int_speed=`bc << EOF
($speed+50) / 100 * 100
EOF
`
percent=`bc << EOF
100.0 * $current / $total
EOF
`
if [ $rate -eq  0 ]; then
  echo $int_speed'MHz, '$percent'%, charging'
else
hours=`bc << EOF
(1.0*$current) / (1.0*$rate) 
EOF
`
minutes=`bc << EOF
60.0 * $current / $rate - $hours * 60
EOF
`
if [ $minutes -lt 10 ]; then
echo $int_speed'MHz, '$percent'%, '$hours':0'$minutes
else
echo $int_speed'MHz, '$percent'%, '$hours':'$minutes
fi
fi

#4 2003-11-19 01:46:13

Guest
Guest

Re: Simple script to get battery charge and time remaining...

zen_guerrilla wrote:

You can also try the 'acpi' pkg, which does exactly the same thing smile.

Considering the acpi package is only dated a few days ago, I don't feel too bad about not knowing about it. big_smile

BTW, here's an update I did to my script.  This one prints minutes less than 10 correctly (1:02 vs 1:2), as well as finding batteries with other names, and printing the CPU speed (nice if you have speed stepping turned on).  It rounds to the nearest 100MHz, which is easy to remove if you don't like it.

#!/bin/bash
speed=`grep "cpu MHz" /proc/cpuinfo | tr -d A-z | tr -d " " | tr -d :`

current=`grep remaining /proc/acpi/battery/*/state | tr -d A-z | tr -d " " | tr -d :`

total=`grep "design capacity:" /proc/acpi/battery/*/info | tr -d A-z | tr -d " " | tr -d :`

rate=`grep "present rate:" /proc/acpi/battery/*/state | tr -d A-z | tr -d " " | tr -d :`

int_speed=`bc << EOF
($speed+50) / 100 * 100
EOF
`
percent=`bc << EOF
100.0 * $current / $total
EOF
`
if [ $rate -eq  0 ]; then
  echo $int_speed'MHz, '$percent'%, charging'
else
hours=`bc << EOF
(1.0*$current) / (1.0*$rate) 
EOF
`
minutes=`bc << EOF
60.0 * $current / $rate - $hours * 60
EOF
`
if [ $minutes -lt 10 ]; then
echo $int_speed'MHz, '$percent'%, '$hours':0'$minutes
else
echo $int_speed'MHz, '$percent'%, '$hours':'$minutes
fi
fi

#5 2010-03-02 13:52:03

samuele.mattiuzzo
Member
From: Treviso, IT
Registered: 2009-10-12
Posts: 307
Website

Re: Simple script to get battery charge and time remaining...

i've tried using it and it just say line 18 and line 29:[ too many arguments...

Offline

#6 2010-03-02 16:10:55

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 835
Website

Re: Simple script to get battery charge and time remaining...

You can also try the 'acpi' pkg

...and acpitool, both do that and much more.


You need to install an RTFM interface.

Offline

#7 2010-03-02 16:41:39

skanky
Member
From: WAIS
Registered: 2009-10-23
Posts: 1,847

Re: Simple script to get battery charge and time remaining...

Yes but to give the OP credit, they did write their script six and a half years ago. wink


"...one cannot be angry when one looks at a penguin."  - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle

Offline

#8 2010-03-03 01:18:22

anrxc
Member
From: Croatia
Registered: 2008-03-22
Posts: 835
Website

Re: Simple script to get battery charge and time remaining...

You mean the script that doesn't work for Samuele (when ACPI interface stayed the same)? Okay, props to the author smile

Last edited by anrxc (2010-03-03 01:26:30)


You need to install an RTFM interface.

Offline

#9 2010-03-03 07:22:51

barzam
Member
From: Sweden
Registered: 2009-01-27
Posts: 277

Re: Simple script to get battery charge and time remaining...

Haha OP is way old.. why resurrect this zombie?

Offline

#10 2010-03-30 01:40:49

cesura
Package Maintainer (PM)
From: Tallinn, Estonia
Registered: 2010-01-23
Posts: 1,867

Re: Simple script to get battery charge and time remaining...

samuele.mattiuzzo wrote:

i've tried using it and it just say line 18 and line 29:[ too many arguments...

Haha 7 year resurrection

Offline

Board footer

Powered by FluxBB