🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: comp.unix.shell
7 messages
7 total messages Started by S P Arif Sahari Wibowo Sat, 20 Sep 2008 05:44
bash history: saving to and restoring from different file
#4021
Author: S P Arif Sahari Wibowo
Date: Sat, 20 Sep 2008 05:44
23 lines
1798 bytes
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!

-- 
                              (stephan paul) Arif Sahari Wibowo
    _____  _____  _____  _____
   /____  /____/ /____/ /____
  _____/ /      /    / _____/      http://www.arifsaha.com/
Re: bash history: saving to and restoring from different file
#4022
Author: S P Arif Sahari Wibowo
Date: Sat, 20 Sep 2008 15:01
39 lines
2328 bytes
On Sat, 20 Sep 2008, jellybean stonerfish wrote:
> From man bash

Thanks. Unfortunately the man page does not give the exact 
moment in a bash session the history is restored from or saved 
to the file. Can be quite helpful information.

> js@blackbox:~$ HISTFILE=/home/js/filename
> js@blackbox:~$ echo $HISTFILE
> /home/js/filename
> js@blackbox:~$ exit

Right. But this is manual intervention. I guess I need to 
clarify that my question is how to do it automatically, i.e. 
just seconds after the history is restored from $HISTFILE 
pointing the file A, $HISTFILE should automatically change to 
file B.

> HISTFILE=filename
> to you .bash_logout

Thanks. But there are 2 issues with this approach:

- Will the history saving happen before or after .bash_logout 
executed?

- That does not help when I use "history -a" in my shell, which 
I do sometime and I am planning to do more often (automated).


Any other idea?

Thanks!

-- 
                              (stephan paul) Arif Sahari Wibowo
    _____  _____  _____  _____
   /____  /____/ /____/ /____
  _____/ /      /    / _____/      http://www.arifsaha.com/
Re: bash history: saving to and restoring from different file
#4023
Author: jellybean stonerfish
Date: Sat, 20 Sep 2008 17:56
53 lines
2963 bytes
On Sat, 20 Sep 2008 11:01:09 -0400, S P Arif Sahari Wibowo wrote:

> On Sat, 20 Sep 2008, jellybean stonerfish wrote:
>> From man bash
> 
> Thanks. Unfortunately the man page does not give the exact moment in a
> bash session the history is restored from or saved to the file. Can be
> quite helpful information.

Try adding a HISTFILE=/home/js/filename to your .bashrc or .bash_profile.
Then open a shell and see what history file was loaded, and also what 
HISTFILE is set to.  Maybe putting the line one of your startup files is 
better than putting it in your .logout file.  With a little trial and 
error, you will figure it out.  If not then try adding a line with
{ sleep 10s; HISTFILE=/home/js/filename } to .bashrc or .bash_profile
that might do it.

> 
>> js@blackbox:~$ 
>> js@blackbox:~$ echo $HISTFILE
>> /home/js/filename
>> js@blackbox:~$ exit
> 
> Right. But this is manual intervention. I guess I need to clarify that
> my question is how to do it automatically, i.e. just seconds after the
> history is restored from $HISTFILE pointing the file A, $HISTFILE should
> automatically change to file B.
> 
>> HISTFILE=filename
>> to you .bash_logout
> 
> Thanks. But there are 2 issues with this approach:
> 
> - Will the history saving happen before or after .bash_logout executed?

Testing it on your system is the best way to answer that question.  Maybe 
setting HISTFILE in a startup file would work better so you can use
history -a .


> 
> - That does not help when I use "history -a" in my shell, which I do
> sometime and I am planning to do more often (automated).

> 
> 
> Any other idea?
> 
> Thanks!

Good luck, and let us know if you figure it out.

stonerfish
Re: bash history: saving to and restoring from different file
#4024
Author: Chris F.A. Johnson
Date: Sun, 21 Sep 2008 00:00
30 lines
2146 bytes
On 2008-09-20, S P Arif Sahari Wibowo wrote:
>
> 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.

hfile1=.bash_history
hfile2=.bash_history2

In your ~/.bashrc:

history -c -r "$hfile1"
history "$hfile2"


-- 
   Chris F.A. Johnson, author       <http://cfaj.freeshell.org/shell/>
   Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
   ===== My code in this post, if any, assumes the POSIX locale
   ===== and is released under the GNU General Public Licence
Re: bash history: saving to and restoring from different file
#4025
Author: S P Arif Sahari Wibowo
Date: Sun, 21 Sep 2008 12:39
24 lines
1692 bytes
On Sun, 21 Sep 2008, Chris F.A. Johnson wrote:
> In your ~/.bashrc:
>
> history -c -r "$hfile1"

Thanks. I did try this before, and the history successfully 
restored just after the command. However, once the .bashrc (and 
.bash_profile) ended, bash still restore from the history file 
adding to the history already there, doubling everything and 
potentially removing the history loaded before (when it passes 
HISTSIZE limit).

> history "$hfile2"

This gives error: numeric argument required


Any other idea? Thanks!

-- 
                              (stephan paul) Arif Sahari Wibowo
    _____  _____  _____  _____
   /____  /____/ /____/ /____
  _____/ /      /    / _____/      http://www.arifsaha.com/
Re: bash history: saving to and restoring from different file
#4026
Author: 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
Re: bash history: saving to and restoring from different file
#4027
Author: S P Arif Sahari Wibowo
Date: Wed, 24 Sep 2008 13:43
47 lines
3253 bytes
On Sun, 21 Sep 2008, jellybean stonerfish wrote:
> 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.

Wow, thanks for the effort to find the solution! I did a test 
and this apparently works: even when the history file exceed 
HISTFILESIZE, the removal of lines (in the beginning of the 
file) apparently done in-place, without creating new file, 
making sure the link is intact (even hard link will work!).

Now, how bash manage to this file modification in-place is 
another interesting question. Anybody know? :-)

Using the link definitely more elegant than what I think. I also 
thinking about delayed action, but instead using link, by 
changing HISTFILE or running history -r in the actual bash 
environment (by using trap).

The obvious issue is obviously the few seconds delay: it is 
posisble another bash session save the history (either by 
history -a or exiting) while the history file still link to the 
temporary file, rendering that saved history lost.

I found that PROMPT_COMMAND is executed after bash deal with 
history. This potentially can be exploited for this purpose if 
the command actually run in the shell environment not in a 
subshell. Need to be tested.

I also start thinking about another way that will avoid this 
issue altogether: each bash session create new history file. 
Before that, a new session will collect all history files from 
previous sessions, sort uniq and read history from the result. 
This method will solve another issue, in which sometime there is 
possible to login without reading the profile file, causing 
HISTFILESIZE not read and the history file truncate to the 
default small size.

I'll let you know how it goes.

Thanks!

-- 
                              (stephan paul) Arif Sahari Wibowo
    _____  _____  _____  _____
   /____  /____/ /____/ /____
  _____/ /      /    / _____/      http://www.arifsaha.com/
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads