shell 判断文件内容是否改变

判断文件内容是否改变:

1)md5值判断

2)diff 判断

#添加日志时间戳
function fn_showlog()
{
    local curtime;
    curtime=`date +"%Y%m%d-%H:%M:%S"`
    echo "$curtime ------ $1";
}
#判断文件内容是否一致
function diff_file(){
    for file in `ls $1`
    do 
        if [ ! -f "$2$file" ];then
           fn_showlog "存在新增文件:$2$file"
           return 0
        else
            diff $file $2$file
            if [ $? -ne 0 ];then
                fn_showlog "文件内容发生变化:$file"
                return 0
            fi
        fi
    done
    return 1
}


diff_file "python*Ip" "/home/admin/tools/"
if [ $? -eq 0 ];then
   fn_showlog "内容发生改变......"
else 
   fn_showlog "内容未发生改变,退出脚本!"
   exit 0
fi

——有的活shell干起来要比java、python、golang更省事!

原文地址:https://www.cnblogs.com/husbandmen/p/12372475.html