-
Notifications
You must be signed in to change notification settings - Fork 0
/
monthly-posts.sh
executable file
·81 lines (67 loc) · 1.51 KB
/
monthly-posts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
# This is for macOS
CMD="$0 $(date +"%Y-%m")"
USAGE=$(cat <<-EOD
USAGE:
$CMD
$CMD -p docs
EOD
)
DESC=$(cat <<-EOD
$USAGE
DESCRIPTION:
The options are as follows:
-p DIR Target directory to create the directory in
-h Display this help
EOD
)
# 初期値の設定
TARGET_DIR="."
# ディレクトリ名が指定されているか確認
if [ $# -eq 0 ]; then
echo "The first argument must be the directory name in YYYY-MM format\n"
echo "$DESC"
exit 1
fi
# 最初の引数が-オプションで始まる場合、エラーメッセージを表示
if [ "${1#-}" != "$1" ]; then
echo "The first argument must be the directory name in YYYY-MM format\n"
echo "$DESC"
exit 1
fi
# 最初の引数を新しいディレクトリ名として設定
NEW_DIR="$1"
shift
# オプション解析
while [ $# -gt 0 ]; do
case "$1" in
-p)
TARGET_DIR="$2"
if [ -z "$TARGET_DIR" ]; then
echo "Target directory not specified.\n"
echo "$DESC"
exit 1
fi
shift 2
;;
-h)
echo "$DESC"
exit 0
;;
*)
echo "Invalid option: $1\n"
echo "$DESC"
exit 1
;;
esac
done
# 作成するディレクトリの完全パスを決定
NEW_DIR_PATH="$TARGET_DIR/$NEW_DIR"
# ディレクトリの作成
mkdir -p "$NEW_DIR_PATH"
# 指定された日付形式のファイルをディレクトリに移動
if mv "${NEW_DIR}"*.md "$NEW_DIR_PATH"; then
echo "Files moved to: $NEW_DIR_PATH"
else
echo "No files to move."
fi