Skip to content

Instantly share code, notes, and snippets.

@baobao

baobao/create_article.sh

Last active Sep 22, 2020
Embed
What would you like to do?
#!/bin/bash
# コピーしたいテンプレート記事パス
template='templates/article_template.md'
# 日付
date=`date '+%Y%m%d'`
# 検索対象フォルダ
dir='drafts/'
pattern="$dir*.md"
files=`find $pattern -type f -maxdepth 0 -name $date*`
max_serial=0
for filepath in $files;
do
echo $filepath
# substr的な文字列抽出
serial_num_str=`echo ${filepath:15:3}`
# 文字列を数字にキャスト
serial_num=$(expr $serial_num_str)
# 連番の最大値を保持
if [ $max_serial -lt $serial_num ];then
max_serial=$serial_num
fi
done
echo "CurrentMaxNo : $max_serial"
# 文字列を数字にキャスト
new_id=$(expr $max_serial + 1)
echo "newID : $new_id"
#3桁になるよう1桁のものは0埋めする00N
new_num=`printf %03d $new_id`
# echo "NewNum : "$new_num
new_base_name=$dir$date$new_num
new_file_path="$new_base_name.md"
echo "COPY : $original => $new_file_path"
cp $template $new_file_path
echo $new_file_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment