Eeepc battery script for 2.6.24 AND 2.6.25


The new kernel for the EeePC (2.6.25) has deprecated the /proc/acpi/battery interface, so I had to write a new script for use in my own zsh prompt.

The script will work in both 2.6.24 and 2.6.25, so without further ado, here it is. It is written as a function for easy inclusion in any prompts.

#!/bin/zsh
bat() {
    PROC=/proc/acpi/battery/BAT0
    SYS=/sys/class/power_supply/BAT0/uevent
    STATE=
    # dc: design capacity, rc: remaining capacity
    if [ -f $PROC/info ]
    then
        STATE=$PROC/state   # 2.6.24
        dc=$(grep 'last full' < $PROC/info | awk '{ print $4 }')
        rc=$(grep 'remaining' < $PROC/state | awk '{ print $3 }')
    elif [ -f $SYS ]
    then
        STATE=$SYS          # 2.6.25
        dc=$(grep '\<power_SUPPLY_CHARGE_FULL\>' < $SYS | awk -F= '{ print $2 }')
        rc=$(grep '\<power_SUPPLY_CHARGE_NOW\>' < $SYS | awk -F= '{ print $2 } ')
    else
        exit
    fi
    p=$(echo 3k $rc $dc / 100 \* p | dc )

    if grep -iq discharging $STATE
    then
        printf " %02d" "$p"
    else
        if [ ${p%.*} -lt 100 ]; then
        printf " %02d+" "$p"
        fi
    fi
}
bat

Update

After some tweaking I even made a better one which saves a few `grep`s.

#!/bin/zsh
bat() {
    PROC=/proc/acpi/battery/BAT0
    SYS=/sys/class/power_supply/BAT0/uevent
    STATE=
    # dc: design capacity, rc: remaining capacity
    if [ -f $PROC/info ]
    then
        STATE=$PROC/state   # 2.6.24
        dc=$(awk '/last full/ { print $4 }' $PROC/info)
        rc=$(awk '/remaining/ { print $3 }' $PROC/state)
    elif [ -f $SYS ]
    then
        STATE=$SYS          # 2.6.25
        dc=$(awk -F= '$1 ~ /^POWER_SUPPLY_CHARGE_FULL$/ { print $2 }' $SYS)
        rc=$(awk -F= '$1 ~ /^POWER_SUPPLY_CHARGE_NOW$/  { print $2 }' $SYS)
    else
        return 0
    fi

    p=$(echo 3k $rc $dc / 100 \* p | dc )

    if grep -iq discharging $STATE; then
        printf " %02d" "$p"
    else
        if [ ${p%.*} -lt 100 ]; then
        printf " %02d+" "$p"
        fi
    fi
}
bat

Update 2

Just found that there is also a nice tool, called acpi…which makes it even more easy:

% acpi -V
Battery 0: Full, 100%
AC Adapter 0: on-line
Thermal 0: ok, 53.0 degrees C
Cooling 0: Processor 0 of 7
code 

See also