Groesbeek, view of the 'National Liberation Museum 1944-1945' in Groesbeek. © Ton Kersten
Fork me on GitHub
Archive for September 2008

Seek in the Kornshell

2008-09-16 (2) by Ton Kersten, tagged as old

Searching for shell builtins to speedup Nanoblogger, we stumbled across a very charming and nice way to implement a tail function in the Kornshell.

This works in KSH93 and newer.

The new redirection operators, <# and ># are used to seek. For example,

   <# (( EOF-36 ))

will seek to 36 bytes before the end-of-file. You can apply this along with any redirection so that

   cat < file <# (( 80 ))

will cat the file starting from offset 80. The value of $(n<#) is the current offset on file descriptor n.

The current syntax can be extended to add other seek options like SEEK_HOLE, by using,

   <# (( HOLE ))

to seek to the next hole.

Eeepc battery script for 2.6.24 AND 2.6.25

2008-09-13 (1) by Ton Kersten, tagged as code

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 greps.

#!/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