文件名autodeploy.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
| #!/bin/bash
blog=myblog dir=/var/www/ path=$dir$blog comm=/root/.nvm/versions/node/v16.15.1/bin/hexo if [ -d "$path" ] then if [ pwd != "$path" ] then cd / fi else echo -e "\n" echo "Error:$blog文件夹不存在,请检查$dir目录下是否存在该文件夹!" echo -e "\n" exit 1 fi cd $path echo -e "\n" echo "成功切换至$path目录下,开始执行部署命令......" echo -e "\n"
if [ ! -f "$comm" ] then echo -e "\n" echo "Error:请检查Hexo命令安装位置后,修改comm变量!" echo -e "\n" exit 2 else $comm clean;$comm g;$comm d fi if [ "$?" -eq "0" ] then echo -e "\n" echo "Hexo已重新发布!" echo -e "\n" else echo -e "\n" echo "执行失败,请检查错误!" echo -e "\n" exit 3 fi
|
如果有更好的建议请发邮件给我哈(ノ´▽`)ノ♪
使用方法
方法一
首先,复制代码根据自己文件路径写好autodeploy.sh文件
然后,进入/root/目录下找到/.bashrc文件,为运行脚本命令添加永久别名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'
alias re='bash /root/autodeploy.sh'
if [ -f /etc/bashrc ]; then . /etc/bashrc fi
|
添加完毕后,输入source ~/.bashrc执行一次文件即可。
以后可以在任意目录下直接输入re命令重新部署网站
方法二
还可以将命令添加到计划任务中
方法三
将脚本直接放在/usr/bin下实现全局执行
更名republish.sh
脚本3.1版本
新增功能,修复line 91: syntax error: unexpected end of file
报错
- 新功能,会在重构完毕后,Push一次
- 修复了一个bug。nnd最后一行少了一个
fi
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 82 83 84 85 86 87 88 89 90
| #!/bin/bash dir=/var/www #设置博客路径 blog=myblog #设置博客根目录名 path=$dir/$blog
resize=800x800 #设置压缩图片质量 qlty=50 #设置压缩图片的程度
hexo=/root/.nvm/versions/node/v16.15.1/bin/hexo #设置hexo命令位置 gulp=/root/.nvm/versions/node/v16.15.1/bin/gulp #设置gulp命令位置 git="/usr/bin/git" #设置git命令位置 # if [ "$1" == 'p' -o "$1" == 'pull' ];then echo "Note: Pulling from remote repo." cd $path $git pull -u origin main fi #0检查路径 if [ -d "$path" ] then if [ pwd != "$path" ] then cd / fi else echo -e "\n" echo "Error:$blog文件夹不存在,请检查$dir目录下是否存在该文件夹!" echo -e "\n" exit 1 fi cd $path echo -e "\n" echo "成功切换至$path目录下,开始执行部署命令......" echo -e "\n" # #1检查hexo是否安装 if [ ! -f "$hexo" ] then echo -e "\n" echo "Error:请检查Hexo命令安装位置后,修改comm变量!" echo -e "\n" exit 2 else $hexo clean; $hexo g; # #优化css,js,html $gulp; if [ "$?" -ne "0" ] then echo -e "\n" echo "Error:请检查gulp命令!" echo -e "\n" exit 3 fi # #压缩图片 echo -e "\n"; echo "开始压缩图片..."; echo -e "\n"; find $dir/$blog/public/img/post -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +100k -print -exec convert -resize $resize -quality $qlty {} {} \;; if [ "$?" -ne "0" ] then echo "Error:请检查压缩图片设置!" exit 4 fi #
#部署到github $hexo d fi if [ "$?" -eq "0" ] then # $git add . $git commit -m "Auto push after republish." $git push -u origin main if [ "$?" -eq "0" ] then echo -e "\n" echo "Hexo已重新发布!" echo -e "\n" else echo -e "\n" echo "执行失败,请检查错误!" echo -e "\n" exit 6 fi fi
|
脚本3.0版本
新增参数
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 82 83 84
| #!/bin/bash dir=/var/www #设置博客路径 blog=myblog #设置博客根目录名 path=$dir/$blog
resize=800x800 #设置压缩图片质量 qlty=50 #设置压缩图片的程度
hexo=/root/.nvm/versions/node/v16.15.1/bin/hexo #设置hexo命令位置 gulp=/root/.nvm/versions/node/v16.15.1/bin/gulp #设置gulp命令位置 git="/usr/bin/git" #设置git命令位置 # if [ "$1" == 'p' -o "$1" == 'pull' ];then echo "Note: Pulling from remote repo." cd $path $git pull -u origin main fi #0检查路径 if [ -d "$path" ] then if [ pwd != "$path" ] then cd / fi else echo -e "\n" echo "Error:$blog文件夹不存在,请检查$dir目录下是否存在该文件夹!" echo -e "\n" exit 1 fi cd $path echo -e "\n" echo "成功切换至$path目录下,开始执行部署命令......" echo -e "\n" # #1检查hexo是否安装 if [ ! -f "$hexo" ] then echo -e "\n" echo "Error:请检查Hexo命令安装位置后,修改comm变量!" echo -e "\n" exit 2 else $hexo clean; $hexo g; # #优化css,js,html $gulp; if [ "$?" -ne "0" ] then echo -e "\n" echo "Error:请检查gulp命令!" echo -e "\n" exit 3 fi # #压缩图片 echo -e "\n"; echo "开始压缩图片..."; echo -e "\n"; find $dir/$blog/public/img/post -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +100k -print -exec convert -resize $resize -quality $qlty {} {} \;; if [ "$?" -ne "0" ] then echo "Error:请检查压缩图片设置!" exit 4 fi #
#部署到github $hexo d fi if [ "$?" -eq "0" ] then echo -e "\n" echo "Hexo已重新发布!" echo -e "\n" else echo -e "\n" echo "执行失败,请检查错误!" echo -e "\n" exit 5 fi #
|
脚本2.0版本
日期:2022/7/30
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
| #!/bin/bash
blog=myblog dir=/var/www path=$dir/$blog comm=/root/.nvm/versions/node/v16.15.1/bin/hexo if [ -d "$path" ] then if [ pwd != "$path" ] then cd / fi else echo -e "\n" echo "Error:$blog文件夹不存在,请检查$dir目录下是否存在该文件夹!" echo -e "\n" exit 1 fi cd $path echo -e "\n" echo "成功切换至$path目录下,开始执行部署命令......" echo -e "\n"
if [ ! -f "$comm" ] then echo -e "\n" echo "Error:请检查Hexo命令安装位置后,修改comm变量!" echo -e "\n" exit 2 else $comm clean; $comm g;
gulp; if [ "$?" -ne "0" ] then echo -e "\n" echo "Error:请检查gulp命令!" echo -e "\n" exit 3 fi $comm clean; $comm g; echo -e "\n"; echo "开始压缩图片..."; echo -e "\n";
find $dir/$blog/public/img/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +200k -print -exec convert -quality 85 {} {} \;; if [ "&?" -ne "0" ] then echo "Error:请检查压缩图片设置!" exit 4 fi $comm d fi if [ "$?" -eq "0" ] then echo -e "\n" echo "Hexo已重新发布!" echo -e "\n" else echo -e "\n" echo "执行失败,请检查错误!" echo -e "\n" exit 3 fi
|
更新内容:
- 加入gulp优化命令
- 加入ImageMagick压缩命令
- 修改了path路径的格式
脚本2.1版本
日期:2022/8/7
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
| #!/bin/bash
dir=/var/www blog=myblog path=$dir/$blog
resize=800x800 qlty=50 hexo=/root/.nvm/versions/node/v16.15.1/bin/hexo gulp=/root/.nvm/versions/node/v16.15.1/bin/gulp
if [ -d "$path" ] then if [ pwd != "$path" ] then cd / fi else echo -e "\n" echo "Error:$blog文件夹不存在,请检查$dir目录下是否存在该文件夹!" echo -e "\n" exit 1 fi cd $path echo -e "\n" echo "成功切换至$path目录下,开始执行部署命令......" echo -e "\n"
if [ ! -f "$hexo" ] then echo -e "\n" echo "Error:请检查Hexo命令安装位置后,修改comm变量!" echo -e "\n" exit 2 else $hexo clean; $hexo g;
$gulp; if [ "$?" -ne "0" ] then echo -e "\n" echo "Error:请检查gulp命令!" echo -e "\n" exit 3 fi
echo -e "\n"; echo "开始压缩图片..."; echo -e "\n"; find $dir/$blog/public/img/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +100k -print -exec convert -resize $resize -quality $qlty {} {} \;; if [ "&?" -ne "0" ] then echo "Error:请检查压缩图片设置!" exit 4 fi
$hexo d fi if [ "$?" -eq "0" ] then echo -e "\n" echo "Hexo已重新发布!" echo -e "\n" else echo -e "\n" echo "执行失败,请检查错误!" echo -e "\n" exit 5 fi
|
更新内容:
- 加入resize参数(图片压缩模块)
- 删除了一些多余的代码
- 添加了注释
脚本2.2版本
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
| #!/bin/bash #功能: #切换至Hexo博客根目录下,执行部署命令 #判断目录是否存在 dir=/var/www #设置博客路径 blog=myblog #设置博客根目录名 path=$dir/$blog
resize=800x800 #设置压缩图片质量 qlty=50 #设置压缩图片的程度 hexo=/root/.nvm/versions/node/v16.15.1/bin/hexo #设置hexo命令位置 gulp=/root/.nvm/versions/node/v16.15.1/bin/gulp #设置gulp命令位置 # #0检查路径 if [ -d "$path" ] then if [ pwd != "$path" ] then cd / fi else echo -e "\n" echo "Error:$blog文件夹不存在,请检查$dir目录下是否存在该文件夹!" echo -e "\n" exit 1 fi cd $path echo -e "\n" echo "成功切换至$path目录下,开始执行部署命令......" echo -e "\n" # #1检查hexo是否安装 if [ ! -f "$hexo" ] then echo -e "\n" echo "Error:请检查Hexo命令安装位置后,修改comm变量!" echo -e "\n" exit 2 else $hexo clean; $hexo g; # #优化css,js,html $gulp; if [ "$?" -ne "0" ] then echo -e "\n" echo "Error:请检查gulp命令!" echo -e "\n" exit 3 fi # #压缩图片 echo -e "\n"; echo "开始压缩图片..."; echo -e "\n"; find $dir/$blog/public/img/ -regex '.*\(jpg\|JPG\|png\|PNG\|jpeg\)' -size +100k -print -exec convert -resize $resize -quality $qlty {} {} \;; if [ "$?" -ne "0" ] then echo "Error:请检查压缩图片设置!" exit 4 fi #
#部署到github $hexo d fi if [ "$?" -eq "0" ] then echo -e "\n" echo "Hexo已重新发布!" echo -e "\n" else echo -e "\n" echo "执行失败,请检查错误!" echo -e "\n" exit 5 fi #
|
更新内容:
- 修复 integer expression expected