#!/bin/bash dir=/data/web [ -f /tmp/md5.list ] && >/tmp/md5.list # /tmp目录是一定存在的,md5.list文件必须清空或删除 find $dir/ -type f > /tmp/file.list while read line do md5sum $line >> /tmp/md5.list # 循环追加 done </tmp/file.list scp /tmp/md5.list B:/tmp/ [ -f /tmp/check_md5.sh ] && rm -f /tmp/check_md5.sh # 要嵌入的脚本文件 cat > /tmp/check_md5.sh << EOF #!/bin/bash dir=/data/web n=`wc -l /tmp/md5.list` # 由于for循环中的文件的行中不能有空格,所有把循环条件转换为文件的行数 for i in `seq 1 $n` # 取变量的值$n,也要脱义,否则就是$n的值 do file_name=`sed -n "$i"p /tmp/md5.list |awk ‘{print $2}‘` md5=`sed -n "$i"p /tmp/md5.list |awk ‘{print $1}‘` if [ -f $file_name ] then md5_b=`md5sum $file_name` if [ $md5_b != $md5 ] then echo "$file_name has changed" fi else echo "$file_name is lost" fi done EOF scp /tmp/check_md5.sh B:/tmp/ ssh B "/bin/bash /tmp/check_md5.sh" #ssh连接B机器后,运行/bin/bash /tmp/check_md5.sh |