O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  

Command Line Battery Monitor
Colorfuly display your ACPI battery status in the command line with a dynamicaly changing $PS1.

Contributed by:
[12/19/05 | Discuss (1) | Link to this hack]

I'm a command line junkie and spend most of my time there. When I use X, I use fluxbox as my window manager because it's fast and lightweight. On my laptop, it's necessary to know what my battery status is for obvious reasons. I've tried using apps such as torsmo and gkrellm, but for me, they defeat my reason for using fluxbox. Since I spend most of my time in the command line, I decided to write a small script whose output would display my battery status directly in my shell prompt. Also, to make it easier to notice, I decided to colorize that part of my prompt, with a different color for different battery levels. You can find this script at http://swicked.net/djaimes/code/bat_mon.sh.To get this output each time my prompt appears, I added this to my $PS1 variable:


So, if your $PS1 was originally this:


Then this is what it should look like now:


To make this change permanent, simply put this line in your .profile

export PS1="\[\$(/usr/local/bin/bat_mon.sh)\]\u@\h:\w\\$ "

Notice the extra backslash (\) before the last $ sign. I found this necessary in order for my prompt to change from $ to # when I did a simple su. Now, each time your prompt is displayed, you can easily see the status of your battery in various colors.

See also:

O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.