🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.unix.shell
Article #4026

Re: bash history: saving to and restoring from different file

#4026
From: jellybean stonerfish
Date: Sun, 21 Sep 2008 18:16
86 lines
4300 bytes
On Sat, 20 Sep 2008 01:44:59 -0400, S P Arif Sahari Wibowo wrote:

> Hi!
> 
> Is there a way for an interactive bash invocation to restore history
> from file A, but later save / append to file B?
> 
> As for the motivation, it is to reduce duplicate in restored history,
> therefore can fit more lines in the history without increasing memory
> per bash invocation. Yes there is erasedups HISTCONTROL option but it
> does not help removing duplicate caused by multiple bash invocation. If
> the file to restore from and file to save to can be different, I can
> create short script to filter out the duplicate from the history file
> and save it in different file. The bash invocation then restore history
> from the filtered file but later still save / append added history to
> the original history file.
> 
> Thanks!

Ok, I may have figured out an ugly way to do what you want.  I turned my 
file "~/.bash_history" into a link, and change where it points to.

We will use two files.  "~/histload" will be the file we load our history
from.  "~/histsave" is the file we save out history to.

First I made a file named "~/histload" with one line.

js@blackbox:~$ cat histload 
echo "This command loaded from histload"

Then I added 2 lines to the end of my .bashrc.

    ln -sf ~/histload ~/.bash_history
    { sleep 2s; ln -sf ~/histsave ~/.bash_history; } &

Now let me try to explain what is happening, as I understand it.
When ".bashrc" is finished being sourced, "~/.bash_history" will 
point to "~/histload"   Then bash will start, and load your history
from "~/histload.  After 2 seconds of sleeping in the background,
"~/.bash_history" will be changed to point to "~/histsave"  So now when 
you do a "history -a" or "exit" your history will save to "~/histsave"

Let's see if it works.  In a terminal I start bash.  Notice the ugly 
output when the sleep command from .bashrc finishes.

    js@blackbox:~$ bash
    js@blackbox:~$ 
    [1]+  Done          { sleep 2s; ln -sf ~/histsave ~/.bash_history; }
    js@blackbox:~$ history
        1  echo "This command loaded from histload"
        2  history
    js@blackbox:~$ echo "This command will save to histsave"
    This command will save to histsave
    js@blackbox:~$ exit
    exit

Now start bash again and confirm that things saved to histsave as planned.

    js@blackbox:~$ bash
    js@blackbox:~$ 
    [1]+  Done          { sleep 2s; ln -sf ~/histsave ~/.bash_history; }
    js@blackbox:~$ history
        1  echo "This command loaded from histload"
        2  history
    js@blackbox:~$ cat histsave 
    history
    echo "This command will save to histsave"
    exit

So all you need to do, is take the good commands you want from "histsave" 
and put them in "histload".  There are a some things you should think 
about.
  The 2 second delay in .bashrc may cause some complications.

  The use of "ln -sf" will erase your current .bash_history file,
  replacing it with a link.

  The ouput of the line
    [1]+  Done          { sleep 2s; ln -sf ~/histsave ~/.bash_history; }
  is ugly.  You may be able to hide it by redirection.  That is up to you 
  to figure out.
  

Does that help?

stonerfish
Technical Details
Message-ID: <2cwBk.63162$4F4.32442@newsfe24.ams2>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!eweka.nl!hq-usenetpeers.eweka.nl!69.16.177.246.MISMATCH!cyclone03.ams.highwinds-media.com!news.highwinds-media.com!npeersf01.ams.highwinds-media.com!newsfe24.ams2.POSTED!8a64a38d!not-for-mail
References: <alpine.OSX.1.10.0809191320370.13605@imac2006.local>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1012a6,6f426515059a57b0
X-Google-Thread: 1054c0,6f426515059a57b0
X-Google-Attributes: gid1012a6,gid1054c0,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!feeder.news-service.com!eweka.nl!hq-usenetpeers.eweka.nl!69.16.177.246.MISMATCH!cyclone03.ams.highwinds-media.com!news.highwinds-media.com!npeersf01.ams.highwinds-media.com!newsfe24.ams2.POSTED!8a64a38d!not-for-mail
From: jellybean stonerfish <stonerfish@geocities.com>
Subject: Re: bash history: saving to and restoring from different file
Newsgroups: comp.unix.shell,gnu.bash
References: <alpine.OSX.1.10.0809191320370.13605@imac2006.local>
User-Agent: Pan/0.129 (Benson & Hedges Moscow Gold)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Lines: 87
Message-ID: <2cwBk.63162$4F4.32442@newsfe24.ams2>
X-Complaints-To: abuse2@dslextreme.com
NNTP-Posting-Date: Sun, 21 Sep 2008 18:16:30 UTC
Organization: DSL Extreme
Date: Sun, 21 Sep 2008 18:16:30 GMT