-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhistory
More file actions
108 lines (88 loc) · 3.52 KB
/
history
File metadata and controls
108 lines (88 loc) · 3.52 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
export HISTFILESIZE=15000
export HISTSIZE=15000
HISTCONTROL=ignoreboth:erasedups
shopt -s histappend
#PROMPT_COMMAND="history -w;$PROMPT_COMMAND"
PROMPT_COMMAND=${PROMPT_COMMAND:-history -a}
_host=$(hostname | awk -F. '{ print $1 }')
_date=$(date '+%m-%d-%y-%s')
HISTFILE="${HOME}/.history/bash-history-${_host}-${_date}"
_epoch_cmd="$HOME/bin/epoch2time-hist.pl"
_hist_usage()
{
error="$*"
test -n && printf 'Error - %s\n\n' ${error}
echo "Usage: hist [-t] [-n X] search-pattern";
echo " -t will disable the more readable time format";
echo " -n X - will print the bare command numbered X from the most recent hist output";
echo " -nn X - will print the command numbered X from the most recent hist output";
}
hist() {
grep_cmd='egrep'
grep_flags=''
grep_str=''
format_time=''
cmd_num=''
debug="0"
[ "$1" = "-d" ] && \
{ debug=1; shift; }
unset CLICOLOR_FORCE
if [ ! -d ${HOME}/.history/ ]; then
echo "Your history files are not setup correctly - ${HOME}/.history/ directory missing!";
return 1;
fi;
kill_comment=""
run_cmd=0
#test -x "${_epoch_cmd}" && format_time="$_epoch_cmd" || { echo "FATAL - missing time format tool, ($_epoch_cmd). Aborting." && return 1; }
test -x "${_epoch_cmd}" || { echo "FATAL - missing time format tool, ($_epoch_cmd). Aborting." && return 1; }
if [ "$1" = "-t" ] ; then
shift
format_time=""
elif [[ "$1" =~ ^-([Nnr]?)([0-9]*)$ ]] ; then
last_arg=${BASH_REMATCH[1]}
[[ -n "${BASH_REMATCH[2]}" ]] && cmd_num=${BASH_REMATCH[2]} || cmd_num=$2
if [[ "$last_arg" = "r" || "$last_arg" = "" ]] ; then
run_cmd=1
elif [[ "$last_arg" =~ ^([Nn]).*$ ]] ; then
[[ "${BASH_REMATCH[1]}" = "n" ]] && kill_comment=1 || kill_comment=0
else
_hist_usage "" && return 1
fi
fi
echo "run_cmd=${run_cmd}"
if [ ! -d ${HOME}/.hist/$$ ] ; then
mkdir -p ${HOME}/.hist/$$
fi
if (( "$#" >= 2 )) ; then
grep_flags=$1
grep_str=$2
elif (( "$#" == 1 )) ; then
grep_str=$1
else
_hist_usage && return 1
fi
# /bin/ls -tr ${HOME}/.history/bash-history-* | xargs -L 65535 ${grep_cmd} ${grep_flags} ${grep_str} | sed 's/.*bash-history-//'
#test -z ${format_time} && format_time="sed 's%.*bash-history-%%'"
test -z ${format_time} && format_time='s#.*bash-history-##'
if [ -n "${cmd_num}" ] ; then
last_hist="$(cat ${HOME}/.hist/$$/last.txt)"
[ -f "${last_hist}" ] || { echo "fatal error: last history file ($last_hist) missing" ; return 2; }
post_grep_re="^[^0-9]${cmd_num}:"
# ls "${post_args[@]}"
[[ ${kill_comment} = 1 || $run_cmd == 1 ]] && post_args=("${post_grep_re}") || post_args=(-B1 "${post_grep_re}")
cmd="$(grep "${post_args[@]}" "${last_hist}" | sed "s/^ ${cmd_num}:/ /")"
[[ $run_cmd == 1 ]] && { sh -c "$cmd" ; return $?; } || echo "$cmd"
else
last_hist="${HOME}/.hist/$$/hist.txt"
echo "${last_hist}" > ${HOME}/.hist/$$/last.txt
echo format_time = "${format_time}"
/bin/ls -tr ${HOME}/.history/bash-history-* | xargs -L 65535 ${grep_cmd} ${grep_flags} ${grep_str} | sed ${format_time} | ${_epoch_cmd} | tee "${last_hist}"
fi
}
fixhist()
{
sudo chown -R "${USER}" "${HOME}/.history/" && \
sudo find "${HOME}/.history" -type f -print0 | sudo xargs -0 chmod 644 && \
sudo chmod 700 "${HOME}/.history"
}
# vim:ft=sh:sts=4:ts=4