bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

lseek with bash


From: Jean-Jacques Brucker
Subject: lseek with bash
Date: Fri, 9 Dec 2011 16:12:15 +0100

Playing with flock to securely access to a file shared by multiple
process. I noticed there are no documented way to do an lseek on an
opened fd with bash :


#!/bin/bash

exec 18<>/tmp/resource
flock 18
# (...) read and analyze the resource file
# ?? there is no documented way to seek or rewind in the resource...
if i redo "exec 8<>/tmp/resource" it close the file descriptor and so
unlock it for flock.... :-(
# write in the resource file
exec 18>&-

I have solve my problem by making this small binary (i just needed a rewind) :

int main(int argc,char * argv[]) { return lseek(atoi(argv[1]),0L,0); }

But i ll be glad to use a standard and finished tool.

Of course we could make an "lseek" binary with some options to cover
all use cases of lseek function. But I prefer to have such
functionality inside bash.

If it does not already exist, here is a proposition :
To understand some characters after a file descriptor, which imply a
lseek (if it is not a pipe or a socket) before reading or writing to
this fd :
looks like :
- $fd[sbae[0-9]*]
-or $fd[+$^-[0-9]*]
were s or ^ imply  whence=SEEK_SET , and [0-9]* is the (positive)
offset in octets (default=0, s for Start or Set)
were b or  - imply  whence=SEEK_CUR , and [0-9]* is the (negative)
offset in octets (default=0, b for Before)
were a or + imply  whence=SEEK_CUR , and [0-9]* is the (positive)
offset in octets (default=0, a for After)
were e or $ imply  whence=SEEK_END , and [0-9]* is the (negative)
offset in octets (default=0, e for End)

and this is how it could be use :
read line <&18s   # do an rewind before to read the (first) line
read -c 3 endchars <&18e4 # read 3 chars before the last one.
echo -n >&18b4 # just move SEEK_CUR 4 octets backward.

What do you think ?

Thx,
Jean-Jacques.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]