MAC环境下,使用Beyond Compare命令行生成两个文件夹差异的html,按目录递归生成。
1#1. 创建compare 2#2. 创建compare/old 3#3. compare/new 4#4. sh tree.sh ~/Desktop/compare 5#5. 生成 ~/Desktop/report 6 7#环境初始化 8function init() 9{ 10 echo "#########环境初始化#########################" 11 12 WORK_DIR=$(cd $(dirname $0); pwd) 13 #echo $WORK_DIR 14 15 BasePath=$1 16 BasePathLength=${#BasePath} 17 18 #命令行是否/结尾问题 19 FINAL=${BasePath:0-1} 20 #echo "FINAL:"$FINAL 21 #echo "BasePath...:"$BasePath 22 if [ ${FINAL} == "/" ]; then 23 #命令行是/结尾 24 BasePath=${BasePath:0:$BasePathLength-1} 25 #echo "BasePath......:"$BasePath 26 BaseOldPath="$1old" 27 BaseNewPath="$1new" 28 echo "YES" 29 else 30 #命令行否/结尾 31 BaseOldPath="$1/old" 32 BaseNewPath="$1/new" 33 echo "NO" 34 fi 35 36 BaseReportPath=$WORK_DIR/report 37 38 #生成report目录 39 if [ ! -d $BaseReportPath ]; then 40 echo "新建目录" 41 mkdir $BaseReportPath 42 else 43 echo "目录存在,重新生成" 44 rm -rf $BaseReportPath 45 mkdir $BaseReportPath 46 fi 47 48 #检查目录 49 if [ -d $BaseReportPath ]; then 50 echo "report目录创建成功" 51 else 52 echo "report目录创建失败,退出" 53 exit 54 fi 55 56 BasePatchPath=$WORK_DIR/diff_to_html 57 58 59 60 echo "强制写入比对配置文件" 61 62 echo "text-report layout:side-by-side & 63 options:display-all,line-numbers,display-context & 64 output-to:%3 output-options:html-color %1 %2" > $BasePatchPath 65 66 if [ -f $BasePatchPath ]; then 67 echo "临时写入比对配置文件成功" 68 else 69 echo "临时写入比对配置文件失败,退出" 70 exit 71 fi 72 73 BaseEmptyPath=$WORK_DIR/empty 74 echo "" > $BaseEmptyPath 75 76 77 echo "##########路径参数###########" 78 echo "BasePath...............:$BasePath" 79 echo "BaseOldPath............:$BaseOldPath" 80 echo "BaseNewPath............:$BaseNewPath" 81 echo "BaseReportPath.........:$BaseReportPath" 82 echo "BasePatchPath..........:$BasePatchPath" 83 echo "BaseEmptyPath..........:$BaseEmptyPath" 84 echo "#####################" 85 86 echo "#########环境初始化 ok############################" 87 88 89} 90 91#目录检查 92# BasePath...............:~/Desktop/compare 93# BaseOldPath............:~/Desktop/compare/old 94# BaseNewPath............:~/Desktop/compare/new 95 96function checkCompareDir(){ 97 98echo "##############checkCompareDir start########################" 99 if [ ! -d $BasePath ]; then 100 echo $BasePath"目录不存在,请检查!" 101 exit 0 102 fi 103 104 if [ ! -d $BaseOldPath ]; then 105 echo $BaseOldPath"子目录不存在,请检查!" 106 exit 0 107 fi 108 109 if [ ! -d $BaseNewPath ]; then 110 echo $BaseNewPath"子目录不存在,请检查!" 111 exit 0 112 fi 113 114echo "##############checkCompareDir end########################" 115 116} 117 118function listFiles() 119{ 120 #1st param, the dir name 121 for file in `ls $1`; 122 do 123 #echo "path....:$1/$file" 124 if [ -d "$1/$file" ]; then 125 # echo "$2$file" 126 # echo "$1/$file" 127 #递归 128 129 listFiles "$1/$file" 130 else 131 132 #比较文件A 133 compareFileAPath=$1/$file 134 #echo "compareFileAPath..............:$compareFileAPath" 135 if [[ "${compareFileAPath}" =~ $BaseNewPath ]]; then 136 #echo "compareFileAPath is new" 137 compareFileBPath=$compareFileAPath 138 compareFileAPath=${compareFileBPath/$BaseNewPath/$BaseOldPath} 139 140 if [ ! -d $compareFileAPath ]; then 141 echo "A path不存在" 142 fulldir=$1 143 #临时html路径,compareFileAPath替换BaseOldPath赋值BaseReportPath 144 tempHtmlPath=${compareFileBPath/$BaseNewPath/$BaseReportPath} 145 # echo "tempHtmlPath....................:$tempHtmlPath" 146 147 #html文件路径,tempHtmlPath拼接.html 148 htmlPath=$tempHtmlPath.html 149 # echo "htmlPath......................:$htmlPath" 150 151 #替换生成Html文件对应的目录 152 htmldir=${fulldir/$BaseNewPath/$BaseReportPath} 153 #echo "htmldir......................:$htmldir" 154 155 #如果Html文件夹不存在,创建对应文件夹 156 if [ ! -d $htmldir ]; then 157 #echo "create.....:$htmldir" 158 mkdir $htmldir 159 fi 160 fi 161 162 else 163 164 echo "#old############" 165 fulldir=$1 166 # echo "fulldir..............:$fulldir" 167 168 #比较文件B,compareFileAPath替换BaseOldPath赋值BaseNewPath 169 compareFileBPath=${compareFileAPath/$BaseOldPath/$BaseNewPath} 170 # echo "compareFileBPath..............:$compareFileBPath" 171 172 #临时html路径,compareFileAPath替换BaseOldPath赋值BaseReportPath 173 tempHtmlPath=${compareFileBPath/$BaseNewPath/$BaseReportPath} 174 # echo "tempHtmlPath....................:$tempHtmlPath" 175 176 #html文件路径,tempHtmlPath拼接.html 177 htmlPath=$tempHtmlPath.html 178 # echo "htmlPath......................:$htmlPath" 179 180 #替换生成Html文件对应的目录 181 htmldir=${fulldir/$BaseNewPath/$BaseReportPath} 182 # echo "htmldir......................:$htmldir" 183 184 #如果Html文件夹不存在,创建对应文件夹 185 if [ ! -d $htmldir ]; then 186 echo "create.....:$htmldir" 187 mkdir $htmldir 188 fi 189 fi 190 191 192 echo "#################file compare start#####################" 193 194 if [ -f $compareFileBPath ]; then 195 #echo "compare aciton...B存在:$compareFileBPath" 196 if [ ! -f $compareFileAPath ]; then 197 echo "#################NEW#####################" 198 bcompare -silent @$BasePatchPath $BaseEmptyPath $compareFileBPath $htmlPath 199 else 200 #A存在 201 echo "#################UPDATE#####################" 202 #echo "compare aciton...$compareFileAPath" 203 # echo "compareFileAPath.......:$compareFileAPath" 204 # echo "compareFileBPath.......:$compareFileBPath" 205 if [[ "${compareFileAPath}" =~ $BaseOldPath ]]; then 206 bcompare -silent @$BasePatchPath $compareFileAPath $compareFileBPath $htmlPath 207 fi 208 fi 209 fi 210 echo "###################file compare end#########################" 211 212 213 214 215 fi 216 done 217} 218 219#清理函数 220function clear(){ 221 #echo "删除比对配置文件" 222 rm -rf $BasePatchPath 223 rm -rf $BaseEmptyPath 224} 225 226#######main run 227 228init $1 229 230checkCompareDir 231 232listFiles $BasePath 233 234clear