需求
windows下开发程序,每次修改完后使用ide自带的插件把代码同步到内网测试服务器,但是每次同步完成后需要手动到服务器执行打包运行等命令,很麻烦,于是就找了一个自动检测文件变动的脚本,目的是每次修改文件后,自动执行命令。
脚本
#!/bin/bash
# Author : cold
# Filename : checkchange.sh
# Useage : sh checkchange.sh [dir]
if [ -f "/tmp/stat.tmp" ]; then
rm -rf /tmp/stat.tmp
fi
if [ -f "/tmp/patch.tmp" ]; then
rm -rf /tmp/patch.tmp
fi
if [ -f "/tmp/nstat.tmp" ]; then
rm -rf /tmp/nstat.tmp
fi
echo "param 1 :"$1
checkisdir(){
# Have one argument
# The argument is a directory
for i in `ls $1 | sed -e 's/ /\n/g'`
do
if [ -d $1/$i ]
then
if [ $i == "node_modules" -o $i == "dist" -o $i == "dist_electron" ] # 不想检测的目录(这里是使用virtualenv生成的环境文件)
then
continue
fi
dir="$1/$i"
checkisdir $dir
else
files=$files'\n'$1'/'$i
fi
done
echo -e $files
}
while true
do
if [ -e /tmp/stat.tmp ]
then
for i in `checkisdir $1`
do
if [ -e /tmp/patch.tmp ]
then
stat $i | grep "更改" > /tmp/nstat.tmp
rm -f /tmp/patch.tmp
continue
fi
stat $i | grep "更改" >> /tmp/nstat.tmp
done
diff /tmp/stat.tmp /tmp/nstat.tmp > /tmp/patch.tmp
if [ $? -eq 0 ]
then
sleep 10
else
echo $(date "+%Y-%m-%d %H:%M:%S")":执行npm run build:dev" # 将此处更改为想要做的操作
cd $1
echo "start run npm run serve"
nohup npm run build:dev > ../build.log &
patch /tmp/stat.tmp /tmp/patch.tmp
fi
else
for i in `checkisdir $1`
do
stat $i | grep "更改">> /tmp/stat.tmp
done
continue
fi
done