日報ファイルをコピーするshellscriptを書いた


日報をMarkdownで書いています。

手作業でコピー・編集していた作業があったのですが、shellscriptを書いて自動化しました。

スクリプト

機能はこんな感じです。

  • 日報ファイルをコピーして今日の日付でファイルを生成します
    • すでに今日のファイルがある場合はコピーしません
  • コピーしたファイルのパスを出力します
  • 一行目を今日の日付で上書き
  • Markdownの完了タスクを消します

(shellscriptあまり書き慣れていないのでツッコミ歓迎しています。)

copy-daily-report.sh
#!/bin/bash -eu

# 前日に書いた日報ファイルをコピー

# * 日報ファイルをコピーして今日の日付でファイルを生成します
#   * すでに今日のファイルがある場合はコピーしません
# * コピーしたファイルのパスを出力します

memoDir="$HOME/Documents/memo/_posts"
dailyReportSuffix="-daily-report.md"
weekTexts=(日 月 火 水 木 金 土)
week=${weekTexts[`date +%w`]}
todayText=`date "+# %Y/%m/%d ($week)"`

cd $memoDir
newestDailyReport=`ls -t *$dailyReportSuffix | head -1`
todayDailyReport=`date +%Y-%m-%d$dailyReportSuffix`

function echoDailyReportPath () {
  echo $memoDir/$todayDailyReport
}

if [ $newestDailyReport = $todayDailyReport ]; then
  echoDailyReportPath
  exit 0
fi

cp $newestDailyReport $todayDailyReport
# * 一行目を今日の日付で上書き
sed -i '' -e "1s|^.*$|$todayText|" $todayDailyReport
# * Markdownの完了タスクを消す
sed -i '' -e "/^ *[-+*] \[[xX]\].*/d" $todayDailyReport

echoDailyReportPath
exit 0

これをpathが通っている場所に置きます。

フォーマットはこんな感じで書いています。

2019-02-02-daily-report.md
# 2019/02/02 (土)

## TODO

* [ ] hoge
* [X] fuga
  * [x] piyo
* [x] 123
* [ ] aaa
  * [ ] bbb

## memo

* あああああ

実行するとこのようなファイルを生成します。

2019-02-03-daily-report.md
# 2019/02/03 (日)

## TODO

* [ ] hoge
* [ ] aaa
  * [ ] bbb

## memo

* あああああ

応用

コピーしたファイルのパスを出力するので、そのままエディターで開くことができます。

# fishにてVSCodeで開く例
code (copy-daily-report.sh)