-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhist.bash
More file actions
executable file
·269 lines (225 loc) · 5.62 KB
/
hist.bash
File metadata and controls
executable file
·269 lines (225 loc) · 5.62 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#!/bin/bash
# ----------------------------------------------------------
# hist
#
# Shows or modifies Bash's history data
#
# Usage: hist[.bash] [options] [--] [[!]keyword ...]]
#
# Author: konsolebox
# Copyright Free / Public Domain
# March 6, 2025
# ----------------------------------------------------------
_DEFAULT_HISTORY_FILE=~/.bash_history
_VERSION=2025.03.06
[ -n "${BASH_VERSION}" ] || {
echo "This script requires Bash."
exit 1
}
function die {
printf '%s\n' "$1"
exit "${2-1}"
}
function show_usage_and_exit {
echo "Shows or modifies Bash's history data
Usage: $0 [options] [--] [[!]keyword ...]]
Options:
-d, --delete Delete matched entries from the history file
-e, -k, --keyword=KEYWORD Alternative way to specify a keyword
-f, --file=FILE Process a different history file
-E, --edit Open history file with an editor
-h, --help Show this usage info and exit
-i, --ignore-case Ignore case when mmatching keywords
-r, --regex Treat keywords as regex patterns
-S, --show-location Show location of the history file
-w, --match-words Match keywords against words found in entries
-V, --version Show version and exit
All entries are shown if no keywords or mode options are specified.
Default history file is '${_DEFAULT_HISTORY_FILE}'."
exit 2
}
function get_opt_and_optarg {
OPT=$1 OPTARG= OPTSHIFT=0
if [[ $1 == -[!-]?* ]]; then
OPT=${1:0:2} OPTARG=${1:2}
elif [[ $1 == --*=* ]]; then
OPT=${1%%=*} OPTARG=${1#*=}
elif [[ ${2+.} ]]; then
OPTARG=$2 OPTSHIFT=1
else
die "No argument specified for '$1'."
fi
return 0
}
function edit_history_file_and_exit {
local file=$1
[[ ${EDITOR-} ]] || die "EDITOR not specified."
set -f
${EDITOR} "${file}"
exit
}
function main {
local delete_mode=false edit=false ignore_case=false history_file=${_DEFAULT_HISTORY_FILE} \
gawk_args=() keywords=() regex_mode=false show_location=false word_mode=false
while [[ $# -gt 0 ]]; do
case $1 in
-d|--delete)
delete_mode=true
;;
-e*|-k*|--keyword|--keyword=*)
get_opt_and_optarg "${@:1:2}"
keywords+=("${OPTARG}")
shift "${OPTSHIFT}"
;;
-E|--edit)
edit=true
;;
-f*|--file|--file=*)
get_opt_and_optarg "${@:1:2}"
history_file=${OPTARG}
shift "${OPTSHIFT}"
;;
-h|--help)
show_usage_and_exit
;;
-i|--ignore-case)
ignore_case=true
;;
-r|--regex)
regex_mode=true
;;
-S|--show-location)
show_location=true
;;
-w|--match-words)
word_mode=true
;;
-V|--version)
echo "${_VERSION}"
exit 2
;;
--)
keywords+=("${@:2}")
break
;;
-[!-][!-]*)
set -- "${1:0:2}" "-${1:2}" "${@:2}"
continue
;;
-?*)
die "Invalid option: $1" 2
;;
*)
keywords+=("$1")
;;
esac
shift
done
function check_conflicting_arguments {
local option_name=$1
shift
for var in delete_mode ignore_case keywords regex_mode word_mode "$@"; do
[[ -z ${!var+.} || ${!var} == false ]] || \
die "Invalid arguments specified along with the ${option_name} option"
done
}
if [[ ${show_location} == true ]]; then
check_conflicting_arguments show-location edit
echo "${history_file}"
return 0
fi
if [[ ${edit} == true ]]; then
check_conflicting_arguments edit show_location
edit_history_file_and_exit "${history_file}"
fi
[[ -e ${history_file} ]] || die "History file doesn't exist: ${history_file}"
[[ -f ${history_file} ]] || die "Histroy file not a file: ${history_file}"
[[ -r ${history_file} ]] || die "Histroy file not readable: ${history_file}"
if [[ ${delete_mode} == true ]]; then
gawk_args=(-i inplace -v delete_mode=1)
[[ ${keywords+.} ]] || die "Delete mode requires keywords to be specified."
fi
[[ ${ignore_case} == true ]] && gawk_args+=(-v ignore_case=1)
[[ ${regex_mode} == true ]] && gawk_args+=(-v regex_mode=1)
[[ ${word_mode} == true ]] && gawk_args+=(-v word_mode=1)
exec gawk "${gawk_args[@]}" '
BEGIN {
if (ARGC > 2) {
for (i = 2; i < ARGC; ++i) {
negate[i] = ARGV[i] ~ /^!/
keywords[i] = negate[i] ? substr(ARGV[i], 2) : ARGV[i]
}
ARGC = 2
} else {
show_all = 1
}
if (ignore_case && regex_mode)
IGNORECASE = 1
}
/^#([[:digit:]]+$)/ {
timestamp = $0
next
}
{
cmd = $0
next_timestamp = ""
if (timestamp) {
lastRT = RT
while (getline > 0) {
if (/^#([[:digit:]]+$)/) {
next_timestamp = $0
break
}
cmd = cmd lastRT $0
lastRT = RT
}
}
if (cmd !~ /^\s*(#|hist\s*)/) {
if (!show_all) {
if (word_mode) {
patsplit(cmd, a, /\w+/)
delete words
for (i in a)
words[ignore_case ? tolower(a[i]) : a[i]] = 1
}
for (i in keywords) {
keyword = keywords[i]
if (regex_mode) {
if (word_mode) {
found = 0
for (word in words)
if ((found = word ~ keyword))
break
} else
found = cmd ~ keyword
} else {
if (ignore_case)
keyword = tolower(keyword)
found = word_mode ? words[keyword] :
index(ignore_case ? tolower(cmd) : cmd, keyword)
}
if (negate[i])
found = !found
if (delete_mode)
found = !found
if (!found)
next
}
}
if (delete_mode) {
if (timestamp)
printf "%s%s", timestamp, ORS
printf "%s%s", cmd, ORS
} else {
if (timestamp)
printf "%5d [%s] %s%s" , ++counter, strftime("%F %T %z", substr(timestamp, 2)),
cmd, ORS
else
printf "%5d %s%s", ++counter, cmd, ORS
}
}
timestamp = next_timestamp
}
' "${history_file}" "${keywords[@]}"
}
main "$@"