以前に、以前にアクセスしたディレクトリを sqlite3 db に記録するスクリプトを作成しました。履歴をすばやく検索してナビゲートするためのショートカットを作成しました。今、bashコマンドで同じことを考えています。
bash でコマンドを実行するとき、コマンド名を取得するにはどうすればよいですか? bash-history の記述を担当する bash のソース コードの一部を変更する必要がありますか? コマンド履歴のデータベースを取得したら、その中でスマート検索を実行できます。
以前に、以前にアクセスしたディレクトリを sqlite3 db に記録するスクリプトを作成しました。履歴をすばやく検索してナビゲートするためのショートカットを作成しました。今、bashコマンドで同じことを考えています。
bash でコマンドを実行するとき、コマンド名を取得するにはどうすればよいですか? bash-history の記述を担当する bash のソース コードの一部を変更する必要がありますか? コマンド履歴のデータベースを取得したら、その中でスマート検索を実行できます。
Advanced Shell Historyツールを使用してシェル履歴を sqlite3 に書き込み、提供されたash_query
ツールを使用してコマンド ラインからデータベースにクエリを実行できます。
vagrant@precise32:~$ 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 は、プレーン テキスト ファイルである ~/.bash_history にすべてのコマンドを既に記録しています。
上下の矢印でコンテンツを参照するか、 を押して検索しcontrol-r
ます。
見てみましょうfc
:
fc: fc [-e ename] [-lnr] [first] [last] または fc -s [pat=rep] [command] 履歴リストからコマンドを表示または実行します。
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 > history.log
または、次を使用して履歴をフラッシュします (BASH によってメモリに保持されているため)。
$ history -a
そして ~/.bash_history を処理します