add timestamp script

This commit is contained in:
松浦 知也 Matsuura Tomoya 2024-02-08 18:01:27 +09:00
parent 490679e0d2
commit 50f4e6759f

29
add_timestamp.sh Executable file
View File

@ -0,0 +1,29 @@
#!/bin/bash
#別のマシンで作ったファイルはわからんので、それぞれのでやって古い方で上書きする必要がある
cwd=`dirname "${0}"`
FILES=$(find $cwd/content -name "*.md")
IFS=$'\n'
for f in $FILES
do
BDATE=$(stat -f "%SB" "$f");
FLINE=$(head -n 1 "$f");
if [[ "$FLINE" == "---" ]];
then
echo "$f has frontmatter";
ORIGINDATE=$(yq --front-matter=extract '.date' "$f" | date "+%s")
LOCALDATE=$(LC_ALL=C date -j -f "%b %d %H:%M:%S %Y" "$BDATE" | date "+%s");
if [ $LOCALDATE -lt $ORIGINDATE ]; then # -lt : '<'
echo "date of local file is older. overwriting."
DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE");
echo "$f : $DATE"
echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f"
fi
else
echo "$f has no frontmatter, adding";
gsed -i '1i---\n---' "$f"
DATE=$(LC_ALL=C date -j -Iseconds -f "%b %d %H:%M:%S %Y" "$BDATE");
echo "$f : $DATE"
echo $DATE | xargs -I{} yq -i --front-matter="process" '.date="{}"' "$f"
fi
done