Saving current directory to bash history

后端 未结 9 1952
时光取名叫无心
时光取名叫无心 2020-11-28 19:29

I\'d like to save the current directory where the each command was issued alongside the command in the history. In order not to mess things up, I was thinking about adding

9条回答
  •  佛祖请我去吃肉
    2020-11-28 20:02

    Gentleman this works better.. The only thing I can not figure out is how to make the script NOT log to syslog on login and log the last command in history. But works like a charm so far.

    #!/bin/bash
    
    trackerbash() {
        # adds comments to bash history entries
    
        # by Dennis Williamson
        # http://stackoverflow.com/questions/945288/saving-current-directory-to-bash-history
        # (thanks to Lajos Nagy for the idea)
    
        #Supper Enhanced by QXT
    
    
        # INSTALLATION: source this file in your .bashrc
    
        export HISTTIMEFORMAT=
    #    export HISTTIMEFORMAT='%F   %T    '
    
        local hcmnt
        local cwd
        local extra
        local thistty
        local whoiam
        local sudouser
        local shelldate
        local TRACKIP
        local TRACKHOST
    
    
                thistty=`/usr/bin/tty|/bin/cut -f3-4 -d/`
                whoiam=`/usr/bin/whoami`
                sudouser=`last |grep $thistty |head -1 | awk '{ print $1 }' |cut -c 1-10`
                hcmnt=$(history 1)
                hcmnt="${hcmnt# *[0-9]*  }"
                cwd=`pwd`
    
    
    
                hcmnt="${hcmnt% ### *}"
                hcmnt=" $hcmnt ${extra:+$extra }"
    
                shelldate=`date +"%Y %b %d %R:%S"`
                TRACKHOST=`whoami | sed -r "s/.*\((.*)\).*/\\1/"`
                TRACKIP=`last |grep $thistty |head -1 | awk '{ print $3 }'`
    
    
                logger -p local1.notice -t bashtracker -i -- "$sudouser ${USER}: $thistty: $TRACKIP: $shelldate: $cwd : $hcmnt"
                history -w 
    
    }
    export PROMPT_COMMAND='trackerbash'
    
    

提交回复
热议问题