log_persistent_history.sh 957 Bytes
Newer Older
maliky's avatar
maliky committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
################ activate persistant history accross terminals

hid_pat='[[:digit:]]+'  #history id
date_pat='[0-9]+-[0-9]+-[0-9]+ [0-9]+:[0-9]+'   # a factoriser
history_pat="^ *$hid_pat *($date_pat) *(.*)"

log_persistent_history() {

    # Save the last history command to ph_file independently of terminal

    if [ ! -f $ph_file ]; then	touch $ph_file;    fi

    # extract parts of the last history
    [[ $(history 1) =~ $history_pat ]]  

    date_time_part="${BASH_REMATCH[1]}"
    cmd_part="${BASH_REMATCH[2]}"
    
    # if the commande was used previously, remove the line and create one with an updated timestamp
    saving_check_pat="$date_pat *$cmd_part *"
    if  egrep -q "$saving_check_pat"  "$ph_file"; then
	sed_check_pat=${saving_check_pat/\//\\\/}
	sed -rie "/$sed_check_pat/d" "$ph_file"
    fi

    echo "$date_time_part $cmd_part" >> $ph_file
}

#### history alias
alias hgrep="history | egrep "
alias phgrep="cat $ph_file | egrep"