Mailinglist Archive: opensuse (4749 mails)

< Previous Next >
Re: [SLE] Short Q
  • From: Vince Littler <suse@xxxxxxxxxxxxxxxxxxxxxxxxx>
  • Date: Fri, 2 May 2003 20:49:56 +0100
  • Message-id: <200305022049.56781.suse@xxxxxxxxxxxxxxxxxxxxxxxxx>
On Friday 02 May 2003 6:51 pm, Martin Guillen wrote:
> Hi all:
>
> If I do: $echo 'lalal' >> /tmp/some_file bash appends lalal to the END of
> the file.
>
> How can I make it so bash adds lalal to the BEGINING of the file?
>
> Thank you?
>
>
>
> PS: I've done it creating a temp file and using >> again, but I want to do
> it with just one command.


Short answer, you probably can't. Without being any kind of shell expert, let
me try and explain what you are trying to do, so that you understand the
problem. The explanation is a bit simplified, so I fully expect that someone
will say I've got some details wrong, but never mind, the principles stand
for dealing with your Q.

A file exists in a directory and is a datastructure which contains 2 kinds of
data. 1] the payload data, which is what you would normally consider to be
the file, and which is transferred when the file is copied or sent somewhere.
2] location data, which says where the data is located on the storage medium,
which is never transmitted, as it is only relevant to the location where the
file is stored.

The location data would be arranged in the form of a linked list [of which
there are many variants], which will consist of 1 or more elements. Each
element consists of 2 pointers [address structures], the first pointing to a
block of payload data, and the second pointing to the next linked list
element. The first element is effectively the file entry in the directory.
The last element points to the data at the end of the file and to NULL as the
next element [ie there is no next element].

When you do: $echo 'lalal' >> /tmp/some_file, this last element is found, and
your text appended to the block if there is space, or a new link list element
added and your text written to the new block. The linked list scheme allows
files to be appended to at the end very easily, without disturbing existing
content. For what you are proposing [adding data at the start of the file],
you would have to shuffle all the data by 5 bytes [plus delimiter characters]
in the first block [plus delimiter characters], and transfer those 5 bytes to
the next block and so on to the end of the file.

As you have discovered, you need a temp file to do this.


HTH

Vince Littler

< Previous Next >
References