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

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.