作業のログを取りたいとき、以前書いたような方法で全部のコマンドを記録することができます。しかし色々とコマンドを試しながら作業を進める場合、全部のコマンドを記録するのは無駄で、作業の各ステップで最終版のコマンドだけを選んで記録したいことがあります。また、それにメモ書きも加えたいところです。これを簡単に行うシェルの関数を紹介します。
When you want to record your operations, you can record every command by means like in my previous entries. But it's often meaningless to record all of your commands if you're trying to figure out how to do something. You may want to pick the final version of the commands for each step of your operation, and then want to add notes to explain what that command does. Here is an easy way to do that by some original functions of the shell.
以下を.bashrcか.zshrcに加えてください。
Add the following to your .bashrc or .zshrc
# Functions to record operations
function rcmd() {
touch commands.log
echo "# ---`date +%F_%T`---" >>commands.log
fc -ln | tail -n 1 >> commands.log
echo "Recorded: `tail -n 1 commands.log`"
}
function rnote() {
touch commands.log
echo "# $@" >> commands.log
echo -e "Recorded: `tail -n 1 commands.log`"
}
function rrun() {
chmod u+x commands.log
./commands.log
}
すると直前のコマンドの記録にはrcmd
、メモの付加にはrnote
が使えるようになります。さらにrrun
で記録したコマンドを実行できます。記録先はカレントディレクトリのcommands.log
です。記録先を選択するのは手間なので決め打ちにしています。以下実行例。
Now you can hit rcmd
to record your last command, rnote
to add notes with it, and rrun
to run saved commands. Commands are recorded to commands.log
in the current directory, which is hard-coded for simplicity.
> echo test
test
> rcmd
Recorded: echo test
> rnote just tested
Recorded: # just tested
> cat commands.log
# ---2017-10-11_21:00:00---
echo test
# just tested
> echo test2
test2
> rcmd
Recorded: echo test2
> rrun
test
test2