shell判断文件是否发生变化

公司有个需求,通过检测文件是否变化,而判断是否进行发布操作,

以下为判断代码

#!/bin/bash
# 检测的文件
package=/data/file.index
# 记录 md5值的文件
md5=package_md5
# 创建新的md5信息
package_md5_new=$(md5sum -b $package | awk '{print $1}'|sed 's/ //g')

# 创建md5的函数
function creatmd5()
{
        echo $package_md5_new > $md5
}

# 判断文件是否存在
if [ ! -f $md5 ] ; then
        echo "md5file is not exsit,create md5file......."
        creatmd5
        exit
fi

# 读取旧的md5信息
package_md5_old=$(cat $md5|sed 's/ //g')

echo $package_md5_new
echo $package_md5_old

# 对象对比判断
if [ "$package_md5_new" == "$package_md5_old" ];then
        echo "md5 is not changed"
        docker restart saas
else
        echo "md5 is  changed"
        creatmd5
        bash ~/fabu.sh
fi



你可能感兴趣的:(shell)