1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2019-02-03

日報を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)
1
1
0

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?