LoginSignup
2
2

More than 5 years have passed since last update.

実行したコマンドを簡単に記録し、メモも付加する。/How to easily record commands you ran and add notes

Last updated at Posted at 2017-10-11

作業のログを取りたいとき、以前書いたような方法で全部のコマンドを記録することができます。しかし色々とコマンドを試しながら作業を進める場合、全部のコマンドを記録するのは無駄で、作業の各ステップで最終版のコマンドだけを選んで記録したいことがあります。また、それにメモ書きも加えたいところです。これを簡単に行うシェルの関数を紹介します。

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
2
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2