-
Notifications
You must be signed in to change notification settings - Fork 0
/
.history_preexec.sh
executable file
·76 lines (69 loc) · 1.44 KB
/
.history_preexec.sh
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
#!/bin/bash
# header guard
[ -n "$_SQLITE_HIST" ] && return || readonly _SQLITE_HIST=1
HISTDB="$HOME/.history.db"
HISTSESSION=`dd if=/dev/urandom bs=32 count=1 2>/dev/null | base64`
__quote_str() {
local str
local quoted
str="$1"
quoted="'$(echo "$str" | sed -e "s/'/''/g")'"
echo "$quoted"
}
__create_histdb() {
if bash -c "set -o noclobber; > \"$HISTDB\" ;" &> /dev/null; then
sqlite3 "$HISTDB" <<-EOD
CREATE TABLE command (
command_id INTEGER PRIMARY KEY,
shell TEXT,
command TEXT,
cwd TEXT,
return INTEGER,
started INTEGER,
ended INTEGER,
shellsession TEXT,
loginsession TEXT
);
EOD
fi
}
preexec() {
[[ ! -v HISTDB ]] && return 0
local cmd
cmd="$1"
#atomic create file if not exist
__create_histdb
local quotedloginsession
if [[ -v LOGINSESSION ]]; then
quotedloginsession=$(__quote_str "$LOGINSESSION")
else
quotedloginsession="NULL"
fi
LASTHISTID="$(sqlite3 "$HISTDB" <<-EOD
INSERT INTO command (shell, command, cwd, started, shellsession, loginsession)
VALUES (
'bash',
$(__quote_str "$cmd"),
$(__quote_str "$PWD"),
$(date +%s%3),
$(__quote_str "$HISTSESSION"),
$quotedloginsession
);
SELECT last_insert_rowid();
EOD
)"
#echo "$cmd" >> ~/.testlog
}
precmd() {
local ret_value="$?"
if [[ -v LASTHISTID ]]; then
__create_histdb
sqlite3 "$HISTDB" <<- EOD
UPDATE command SET
ended=$(date +%s%3),
return=$ret_value
WHERE
command_id=$LASTHISTID ;
EOD
fi
}