以前我寫過一個腳本,它將以前訪問過的目錄記錄到sqlite3數據庫中。我寫了一些快捷方式來快速搜索和瀏覽歷史。現在我正在考慮用我的bash命令做同樣的事情。在數據庫中記錄bash歷史記錄
當我在bash中執行命令時,如何獲得命令名?我是否必須更改bash的源代碼中負責編寫bash-history的部分?一旦我有我的命令歷史數據庫,我可以在其中進行智能搜索。
以前我寫過一個腳本,它將以前訪問過的目錄記錄到sqlite3數據庫中。我寫了一些快捷方式來快速搜索和瀏覽歷史。現在我正在考慮用我的bash命令做同樣的事情。在數據庫中記錄bash歷史記錄
當我在bash中執行命令時,如何獲得命令名?我是否必須更改bash的源代碼中負責編寫bash-history的部分?一旦我有我的命令歷史數據庫,我可以在其中進行智能搜索。
在fc
請看:
FC:FC [-e參數] [-lnr] [第一] [最後]或FC -s [PAT =代表] [命令] 顯示或執行來自歷史列表的命令。
fc is used to list or edit and re-execute commands from the history list. FIRST and LAST can be numbers specifying the range, or FIRST can be a string, which means the most recent command beginning with that string. Options: -e ENAME select which editor to use. Default is FCEDIT, then EDITOR, then vi -l list lines instead of editing -n omit line numbers when listing -r reverse the order of the lines (newest listed first) With the `fc -s [pat=rep ...] [command]' format, COMMAND is re-executed after the substitution OLD=NEW is performed. A useful alias to use with this is r='fc -s', so that typing `r cc' runs the last command beginning with `cc' and typing `r' re-executes the last command. Exit Status: Returns success or status of executed command; non-zero if an error occurs.
你可以調用它去插入到表中的文本,但爲什麼,如果它已經被Bash保存煩惱呢?使用
$ history > history.log
或沖洗歷史(因爲它是被保持在由BASH存儲器)::
爲了得到完整的歷史或者使用歷史命令和處理其輸出
$ history -a
然後過程〜/ .bash_history
對不起,這麼晚來到這個問題!
我傾向於在我工作的地方運行很多shell,結果長時間運行的shell歷史記錄會一直混合或丟失。我終於受夠了,我開始記錄到數據庫:)
我還沒有完全制定出整合,但這裏是我的設置:
在所有這些命令的結尾,所有的命令都應該被記錄到名爲systemevents的表中。你一定會想建立的索引上幾個字段,如果你經常使用shell作爲查詢可以開始採取永遠:)
這裏有幾個指標的我設置:
指標: 「systemevents_pkey」 PRIMARY KEY,B樹(ID) 「systemevents_devicereportedtime_idx」 B樹(devicereportedtime) 「systemevents_fromhost_idx」 散列(fromhost) 「systemevents_priority_idx」 B樹(優先級) 「systemevents_receivedat_idx」 B樹(receivedat)
fromhost,receivedat ,和德維cereportedtime特別有幫助!
從短短的一段時間我一直在使用它,這真是太神奇了。它讓我能夠在最近的任何服務器上找到命令!不要再失去一個命令!如果您有多個用戶,也可以將它與停機時間/其他問題關聯起來。
我正計劃編寫我自己的rsyslog插件,以使數據庫中的歷史記錄格式更加有用。我更新時,當我:)
祝你好運!
您可以使用Advanced Shell History工具將您的shell歷史寫入sqlite3,並使用提供的ash_query
工具從命令行查詢數據庫。
[email protected]:~$ ash_query -Q
Query Description
CWD Shows the history for the current working directory only.
DEMO Shows who did what, where and when (not WHY).
ME Select the history for just the current session.
RCWD Shows the history rooted at the current working directory.
您可以編寫自己的自定義查詢,並使其可從命令行使用。
這個工具給你很多的額外的歷史信息之外的命令 - 你退出代碼,開始和停止時間,當前的工作目錄,TTY等
充分披露 - 我是作者和維護者。
不太清楚bash的內置歷史是什麼,但如果你真的想要這個,切換到zsh,你可以擁有更多的權力和控制權。 – Kevin