不同session的history操作同步问题

惊恐openssh不同时间开的不同session,相同用户的history命令显示的是不完全相同的…不过bash的设计是有它的考虑的...要不chsh到zsh用用...安静

可能影响session的history选项就如下几个:

不同session的history操作同步问题_第1张图片

简单源码了解

源码看,暂时没有查出什么时候会统一写入 .bash_history文件!小坑要小心呀委屈

不同session的history操作同步问题_第2张图片

几个简单的实验,不同session的bash history是不同步的!

处理方式1

参考地址

不同session的history操作同步问题_第3张图片

处理方式2

参考地址

四个脚本备忘://注意将.bashrc.user&bash_history_all_append.sh在.bashrc或其他登录shell中添加执行.

1. ${HOME}/.bashrc.user

if [[ `tty` != "not a tty" ]] ; then # interactive shell

  ##################################
  # BEG History manipulation section
    export TTY_NAME=`tty|sed -e 's|/dev/||' -e 's|/|_|'`
    export HISTFILESIZE=2000
    export HISTSIZE=2000
    export HISTTIMEFORMAT="[%Y-%m-%d %H:%M:%S] "
    export HISTFILE=$HOME/.bash_history.${TTY_NAME}
    rm -f $HISTFILE

    tail --lines=$(( $HISTFILESIZE + 200 )) ${HOME}/.bash_history_all | sort -u -m -k 4  | tail --lines=$HISTFILESIZE |awk -f ~/.bash.parse_history.awk >> $HISTFILE

    history -r

    if [ -n "$PROMPT_COMMAND" ]; then
        #It is annoying that in PROMPT_COMMAND $HISTCMD is always 1 hence the external script
        export PROMPT_COMMAND="$PROMPT_COMMAND; ${HOME}/.bash_history_all_append.sh \`history 1\`"
    else
        export PROMPT_COMMAND="${HOME}/.bash_history_all_append.sh \`history 1\`"
    fi

    function save_last_command {
        # Only want to do this once per process
        if [ -z "$SAVE_LAST" ]; then
            export SAVE_LAST="done"
            ${HOME}/.bash_history_all_append.sh "`history 1`"
            echo "${TTY_NAME} [`date +'%m-%d-%Y_%T'`] # end session $USER@${HOSTNAME}:`tty`" >> ${HOME}/.bash_history_all
            rm -f $HISTFILE
        fi
    }
    trap 'save_last_command' EXIT

  # END History manipulation section #################################
fi
2.  ${HOME}/.bash.parse_history.awk

#!/usr/bin/awk -f
# Awk script: extract.awk

function extract(str,regexp)
{ RMATCH = (match(str,regexp) ? substr(str,RSTART,RLENGTH) : "")
 #print str " " RSTART " " RLENGTH
 return RSTART }

function after(str,regexp)
{ AMATCH = (match(str,regexp) ? substr(str,RSTART+RLENGTH) : "")
 #print str " " RSTART " " RLENGTH
 return RSTART }


extract($0,"[0-9][0-9]+-[0-9]+-[0-9][0-9]+[_ ][0-9][0-9]:[0-9][0-9]:[0-9][0-9]") {
   gsub(/_/," ", RMATCH)
   TIME = RMATCH
}

after($0,"[0-9][0-9]+-[0-9]+-[0-9][0-9]+[_ ][0-9][0-9]:[0-9][0-9]:[0-9][0-9][] ]+") {
   CMD = AMATCH
}


{
 #printf("TIME=%s,CMD=%s\n",TIME,CMD)
 TRANSDATE = "date --date \"" TIME "\" +%s 2>/dev/null"
 TRANSDATE | getline EPOC
 #printf ("%s\n#%s\n",CMD,EPOC)
 printf ("#%s\n%s\n",EPOC,CMD)
}
3. ${HOME}/.bash_history.sed

s/^\s*[[:digit:]]*\s*//
s/\\/\\\\/g
s/"/\\"/g
s/'/\\'/g
4. ${HOME}/.bash_history_all_append.sh 

#!/usr/bin/env bash

HISTORY_LOG=${HOME}/.bash_history_all

LC=`echo "$*" | sed -f ~/.bash_history.sed | xargs -i -n 1 echo ${TTY_NAME} '{}'`;
if [[ -z `tail -100 ${HISTORY_LOG} | grep -F "$LC" 2>/dev/null` ]] ; then
    echo "${LC}" >> ${HISTORY_LOG}
fi
5. cat ~./bash_history_all即可.

可以看到不同终端的不同命令,当然还有命令执行的时间~nice.

^.^

疑问……









你可能感兴趣的:(history)