shell

对比二个目录下文件是否存在差异

 1 #!/bin/bash
 2 # V1.0
 3 # 脚本开发 BUY EMAIL:"tzwken@126.com" QQ:286452529
 4 
 5 . /etc/profile >/dev/null 2>&1
 6 PATH="$PATH:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
 7 
 8 export LC_ALL=en_US.UTF-8
 9 export LANG=en_US.UTF-8
10 export PATH
11 rc=0
12 
13 usage_() {
14             echo "Usage: $0 -m host1 -s host2 -p path      对比host1和host2的path目录下的文件"
15             exit 1
16 }
17 
18 while getopts "m:s:p:h" arg #选项后面的冒号表示该选项需要参数
19 do
20     case $arg in
21         m)
22             host1=${OPTARG}
23             ;;
24         s)
25             host2=${OPTARG}
26             ;;
27         p)
28             path=${OPTARG}
29             ;;
30         h)
31             usage_
32             ;;
33         ?)  #当有不认识的选项的时候arg为?
34             echo "unkonw argument"
35             exit 1
36         ;;
37     esac
38 done
39 
40 shift $((OPTIND-1))
41 
42 if [ "X${path}" == "X" ] || [ "X${host1}" == "X" ] || [ "X${host2}" == "X" ];then
43     usage_
44     exit 2
45 fi
46 
47 host1md5=/tmp/.filediff_h1.md5
48 host2md5=/tmp/.filediff_h2.md5
49 
50 ssh ${host1} "
51 if [ -d ${path} ];then
52     cd ${path} && find -type f -exec md5sum {} ;
53     for i in \`find -type l\`;do if [ -f $i ];then md5sum $i; elif [ -d $i ];then file $i;fi;done
54 fi
55 "|sort -k2 >${host1md5}
56 
57 ssh ${host2} "
58 if [ -d ${path} ];then
59     cd ${path} && find -type f -exec md5sum {} ;
60     for i in \`find -type l\`;do if [ -f $i ];then md5sum $i; elif [ -d $i ];then file $i;fi;done
61 fi
62 "|sort -k2 >${host2md5}
63 
64 if [ -f ${host1md5} ] && [ -f ${host2md5} ];then
65     diff ${host1md5} ${host2md5}
66 fi
67 rm -f ${host1md5} ${host2md5}
View Code
原文地址:https://www.cnblogs.com/you0329/p/6895196.html