1r Guidelines

All patches and comments are welcome. Please squash your changes to logical commits before using git-format-patch and git-send-email to patches@git.onerussian.com. If you'd read over the Git project's submission guidelines and adhered to them, I'd be especially grateful.

If you use my repositories frequently, consider adding the following snippet to ~/.gitconfig and using the third clone URL listed for each project:

[url "git://git.onerussian.com/"]
  insteadOf = 1r:

Merge branch 'master' of ssh://git.onerussian.com/~/git/pub/etc/bash
[etc/bash.git] / .bash / bashrc / 50_infinite_history
1 #emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
2 #ex: set sts=4 ts=4 sw=4 noet:
3
4 #-------------------------- =+- Shell script -+= --------------------------
5 # @file      .bashrc_history.sh
6 # @date      Thu Mar 10 14:02:36 2005
7 # @brief
8 #
9 # Created: Thu Mar 10 14:02:36 2005
10 #
11 #  Yaroslav Halchenko                                      CS@UNM, CS@NJIT
12 #  web:     http://www.onerussian.com                      & PSYCH@RUTGERS
13 #  e-mail:  yoh@onerussian.com                              ICQ#: 60653192
14 #
15 # DESCRIPTION (NOTES):
16 #   A script to be sourced from .bashrc to provide ways to archive all the
17 #   actions in infinitely long history file.
18 #
19 #   To use, just place
20 #     '[ -r ~/.bashrc_history ] && source ~/.bashrc_history' in ~/.bashrc
21 #
22 #     '$if Bash
23 #      # to exit through calling exit function which will archive the history
24 #      # next ones are optional: first is left for historical reasons
25 #      "\C-x\C-x": "exit\n"
26 #      "\C-x\C-w": "archive_history\n"
27 #      $endif'                   in ~/.inputrc
28 #
29 #   Then whenever you close bash (exit,logout or exit by pressing Ctrl-D
30 #   or Ctrl-X twice you will have a piece of current history added to
31 #   ~/.bash_history.archive
32 #
33 #   Thanks to Philippe Teuwen for the comments and "patches"
34 #
35 # SOURCE (might be outdated):
36 #   http://www.onerussian.com/Linux/.files/.bashrc_history
37 #
38 # LICENSE:
39 #   Released under GNU Generic Public License. You should've received it with 
40 #   your GNU/Linux system. If not, write to the Free Software Foundation, Inc.,
41 #   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
42 #-----------------\____________________________________/------------------
43
44 # For easy migration under modular bash configuration (after madduck's
45 # zsh configuration) -- if we find older archive file and now HISTFILE
46 # points to $HOME/.var, we need to move things around
47 if [ "$HISTFILE" != "" ] &&  [ "$PS1" ] \
48         && [ -f $HOME/.bash_history ] && [ "$BASH_VARDIR" = "$HOME/.var/bash" ]; then
49         if [ ! -f "$BASH_VARDIR/history" ] ; then
50                 echo -e "I: Found previousely allocated history files.\n   Moving them under $BASH_VARDIR"
51                 for f in $HOME/.bash_history*; do
52                         mv "$f" "${f//.bash_/.var/bash/}"
53                 done
54         else
55                 echo -e "I: New layout of history files also found.\n   Please remove/propagate old history into new"
56         fi
57 fi
58
59 if [ "$PS1" ] ; then # interactive shell
60     export STARTTIME="`date '+%Y-%m-%d %H:%M:%S'`" \
61            HISTORYDUMP=0 \
62            HISTTIMEFORMAT='%F %T '
63     shopt -s cmdhist histappend
64
65     # Next function really stores the history logs. Besides that if you want to store whatever you have so far
66     # you can call this function and it will save increment from the last call to it.
67     archive_history()
68     {
69         TFILE=${HISTFILE}.$$.temp
70         CURTIME=`date '+%Y-%m-%d %H:%M:%S'`
71                 CURTTY=`tty`
72         #STAMP
73         HISTORYDUMP=$(($HISTORYDUMP+1))
74         # Title
75         echo "#$USER@${HOSTNAME} [ ${STARTTIME} - ${CURTIME} ]:$HISTORYDUMP ($CURTTY) ----" >| $TFILE
76         # Fresh tasty history
77                 history -a ${TFILE}
78         # Append it to the archive
79         #cat ${TFILE} >> ${HISTFILE}.archive
80         which gawk >/dev/null \
81          && awk '/^#[0-9]+$/{print strftime("#%F %T ", gensub(/#/,"","g")) }//' ${TFILE} >> ${HISTFILE}.archive \
82          || cat ${TFILE} >> ${HISTFILE}.archive
83
84         # Adjust history file itself
85         # of cause we need to dump to some temp file and then rename... he heh
86         cat ${HISTFILE} ${TFILE} | tail -${HISTSIZE} >| ${HISTFILE}.$$
87         mv -f ${HISTFILE}.$$ ${HISTFILE}
88         # clean up after yourself - to be absessive remove the other tmp file as well -
89         # - might happen if there were no permission to move it at above point
90         rm -rf ${TFILE} ${HISTFILE}.$$
91     }
92
93     trap 'archive_history' EXIT HUP
94     unset IGNOREEOF
95
96 fi
97
98 # ---------------{ some shortcuts }---------------------
99 gbha ()
100 {
101     grep "$@" ${HISTFILE}.archive
102 }
103
104 gbhaa ()
105 {
106     grep -h "$@" ${HISTFILE}.archive{,s/.*}
107 }