I.T. Security and Linux Administration

Jun 24 2011   2:51PM GMT

Simple battery check script

Eric Hansen Eric Hansen Profile: Eric Hansen

Like most people, I have Linux installed on my laptop. However, with the way I have it set up, I don’t have a readily-available battery monitoring widget/app/etc… to tell me when my battery’s going to die. Even though my laptop is almost always on the charger now, I still decided to cook up a little script to make this a little more aware to me, seeing as how I almost always have the terminal open.

This is yet another famous Bash script I’ve written. What you need first is to install ACPI and awk if you haven’t already (this can probably be done via /proc access too, but that’ll be another time). After that, when you run acpi -V you’ll get some output in regards to your battery consumption (as well as fans). The first line is what we want to make use of. Seeing as how WordPress doesn’t fancy shell scripting languages, I’ll be using the PHP highlighting since it’s another one of my favorite languages.


# As stated, we only want the first line, thus we use head
ACPI=`acpi -V | head -n 1`

# Different information to return
STATE=`echo -n "$ACPI" | awk '{print $3}'`
AMOUNT=`echo -n "$ACPI" | awk '{print $4}'`
TIME=`echo -n "$ACPI" | awk '{print $5}'`

echo "Battery is $STATE ($AMOUNT) with $TIME left."

exit 0

I learned the hard way a while ago you can’t just do something like `$ACPI | awk…`, it just won’t work. This will output something like: “Battery is Charging, (75%,) with 05:03:23 left.” Now, having those commas in there will make this look quite ugly. That’s why I modified it a little bit for my shell prompt.

function batt_call () {
BATT=`acpi -V | head -n 1 | awk '{print substr($4,0,length($4)-1)}'`

echo -ne $BATT

PS1='${batt_call) \$ '

The big change here is the substr() inside of the print function for awk. What you do is take the text you want to get the string from (in this case, $4 is the percentage [“76%,”]), then where to start (0 = beginning), and if you want to return something before the end of the string, you specify the end. Since the string of $4 can be anywhere from 2 (1%) to 4 (100%), we get the current length of the string (length()) and subtract 1 so we don’t return the comma. The output here will now be: “76% $” for the shell prompt (minus the quotations).

Another variant of the first script is to do the following:


# As stated, we only want the first line, thus we use head
ACPI=`acpi -V | head -n 1`

# Different information to return
STATE=`echo -n "$ACPI" | awk '{print substr(tolower($3), 0, length($3)-1)}'`
AMOUNT=`echo -n "$ACPI" | awk '{print substr($4, 0, length($4)-1)}'`
TIME=`echo -n "$ACPI" | awk '{print $5}'`

if [ "$STATE" == "charging" ]
ENDING="until fully charged."
ENDING="until a recharge is needed."

echo "Battery is $STATE ($AMOUNT) with $TIME left $ENDING."

exit 0

This will give the output similar to “Battery is charging (85%) with 00:36:09 left until fully charged.” The only change (besides the substr() & length()) is the tolower() function. This is done because the output is originally “Charging”, for example, which wouldn’t look good in this script if you are picky about your English.

I’m not a big fan of this process since it’s mading continuous calls to a program (and I like to keep my reads & writes as minimal as possible), but it gets the job done.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: