hexo-scripts

针对hexo-next搭建的博客,写了几个方便操作的脚本:

  • new.sh: 任意目录新建带模板的md文件
  • post.sh: 任意目录post+deploy
  • backup.sh: 备份博客,以便日后迁移
1
alias new=your_path/new.sh

任意目录下new 123-456

会生成123-456.md文件并mv到当前目录下。

1
2
3
4
5
6
7
8
#!/bin/bash
blog_PATH=/home/zzp/work/blog
CURRENT=$PWD
cd $blog_PATH
# echo “Usage: new.sh title”
hexo n $1
echo “[+] mv $1.md to current directory …”
mv $blog_PATH/source/_posts/$1.md $CURRENT
1
alias post=your_path/post.sh

运行参数

1
2
3
4
5
6
7
8
9
10
# 将文件post到hexo博客source/_post下指定分类中
./post.sh [file] -c “algorithm/leetcode”
# deploy your website.
./post.sh -d
# local server
./post.sh -p 9920
# post + deploy
./post.sh -d [file]
# post + local server
./post.sh -p 9920 [file]

脚本内容

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
#!/bin/bash

blog_PATH=/home/zzp/work/blog
post_PATH=$blog_PATH/source/_posts
# update to web if DEPLOY=1
DEPLOY=0
PORT=0

if [ $1 = “-h” ]||[ $1 = “–help” ];then
echo “Usage: basename <span class="variable">$0</span> mdfile1 mdfile2 …”
echo “Options:”
echo “-d, –deploy: deploy your website.”
echo “-p, –port: port of local server.”
echo “-c, –category: category of your post.”
fi

if [ $# = 0 ];then
cd $post_PATH
fi

POSITIONAL=()
while [[ $# -gt 0 ]]
do
key=$1

case $key in
-d|–deploy)
DEPLOY=1
shift # past argument
;;
-p|–port)
PORT=$2
shift # past argument
shift # past value
;;
-c|–category)
CAT=$2
shift
shift
;;
*) # unknown option
POSITIONAL+=($1) # save it in an array for later
shift # past argument
;;
esac
done
set${POSITIONAL[@]} # restore positional parameters


for i in $@; do
if [ -f $i ];then
mv $i $post_PATH/$CAT
echo “[+] move $i into $post_PATH/$CAT …”
fi
done


# hexo clean and generate.
if [ $DEPLOY = 1 ]||[ $PORT != 0 ];then
cd $blog_PATH
# hexo clean
hexo g
fi

# hexo deploy your website.
if [ $DEPLOY = 1 ];then
hexo d
else
# local server port.
if [ $PORT != 0 ];then
hexo s -p $PORT
fi
fi

打包hexo博客项目内需要备份的文件

1
2
3
4
5
6
7

#!/bin/bash
blog_PATH=/home/zzp/work/blog
cd $blog_PATH
# backup
tar zcvf “blogsource.`date +%Y%m%d`.tar.gz” scaffolds/ source/ themes/ .gitignore _config.yml package.json *.sh backup