[lug] Random access from the shell

Jeffrey Haemer jeffrey.haemer at gmail.com
Wed Nov 12 10:03:44 MST 2008


Yesterday, Jeremy and I were talking about how to do random access I/O from
the shell.  Here's an example script, in case you ever need to do it:

#!/bin/bash

## "seek foo 4" seeks to character 4 (0-based) in file "foo"
seek() {
  exec 3<> $1
  [ $2 -eq 0 ] && return
  read -n $2 <&3
}

## write "some string" writes the string at the current position in the file
you've just been seeking in
write() {
  printf $1 >&3
}

## now, an example
echo hello, world > foo  # a test file to overwrite
seek foo 7
write "Jeff!"
seek foo 0
write "Howdy"

-- 
Jeffrey Haemer <jeffrey.haemer at gmail.com>
720-837-8908 [cell]
http://goyishekop.blogspot.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20081112/f8609a63/attachment.html>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: rewrite.txt
URL: <http://lists.lug.boulder.co.us/pipermail/lug/attachments/20081112/f8609a63/attachment.txt>


More information about the LUG mailing list