-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_md5_of_two_folders.sh
More file actions
174 lines (162 loc) · 4.39 KB
/
diff_md5_of_two_folders.sh
File metadata and controls
174 lines (162 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
if [ $# -ne 2 ];then
echo "Usage:$0 dir1 dir2"
exit 1
fi
if [ ! -d "$1" -o ! -d "$2" ];then
echo "$1 or $2 is not derectory!"
exit 1
fi
## 获取绝对路径
arg1=`readlink -f "$1"`
arg2=`readlink -f "$2"`
tmp_dir=$PREFIX/tmp/tmp.$$
rm -rf $tmp_dir
mkdir -p $tmp_dir || exit 0
#echo $tmp_dir
trap "rm -rf $tmp_dir; exit 0" SIGINT SIGTERM
## 获取MD5
function get_file_md5
{
if [ $# -ne 1 ];then
echo "get_file_md5 arg num error!"
return 1
fi
local file=$1
md5sum "$file" | awk -F" " '{print $1}'
}
function myexit
{
rm -rf $tmp_dir
exit 0
}
function show_diff
{
if [ $# -ne 1 ];then
return 1
fi
local diff_file=$1
echo "diff file:"
printf " %-55s %-52s\n" "$arg1" "$arg2"
if [ -f $tmp_dir/A_only_file ];then
awk '{printf(" [%2d] %-50s\n", NR, $0)}' $tmp_dir/A_only_file
python -c 'print("-"*100)'
fi
if [ -f $tmp_dir/B_only_file ];then
awk '{for(i=0;i<60;i++)printf(" "); printf(" [%2d] %-50s\n",NR, $0)}' $tmp_dir/B_only_file
python -c 'print("-"*100)'
fi
if [ -f $diff_file ];then
awk '{printf(" [%2d] %-50s %-50s\n", NR, $0, $0)}' $diff_file
echo "(s):show diff files (a):open all diff files (q):exit"
echo
fi
}
function check_value
{
local diff_file=$1
local value=$2
tmp_file=$tmp_dir/tmp_file
>$tmp_file
for numbers in `echo "$value" | tr ',' ' '`
do
nf=`echo "$numbers" | awk -F"-" '{print NF}'`
if [ $nf -ne 1 -a $nf -ne 2 ];then
return 1
fi
begin=`echo "$numbers" | awk -F"-" '{print $1}'`
end=`echo "$numbers" | awk -F"-" '{print $2}'`
if [ -z "$end" ];then
sed -n $begin'p' $diff_file >> $tmp_file
else
if [ "$end" -lt $begin ];then
return 1
fi
sed -n $begin','$end'p' $diff_file >> $tmp_file
fi
if [ $? -ne 0 ];then
return 1
fi
done
awk -v dir1="$arg1" -v dir2="$arg2" '{
if (NR==1)
{
printf("edit %s/%s\nvertical diffsplit %s/%s\n", dir1, $0, dir2, $0)
}
else
{
printf("tabnew %s/%s\nvertical diffsplit %s/%s\n", dir1, $0, dir2, $0)
}
}' $tmp_file
}
#############################################################
# 获取diff info
#############################################################
for file1 in `find "$arg1" | sed 's% %#%g'`
do
file=`echo $file1 | sed 's%#% %g'`
file_relative_name=${file#$arg1/}
file "$file" | grep -Eqv "directory"
if [ $? -ne 0 ];then
continue
fi
if [ -f "$arg2/$file_relative_name" ];then
file "$arg2/$file_relative_name" | grep -Eqv "directory"
if [ $? -ne 0 ];then
continue
fi
md5_1=`get_file_md5 "$file"`
md5_2=`get_file_md5 "$arg2/$file_relative_name"`
if [[ "$md5_1" = "$md5_2" ]];then
continue
fi
## file not same
echo "$file_relative_name" >> $tmp_dir/diff_file
else
echo "$file_relative_name" >> $tmp_dir/A_only_file
fi
done
for file1 in `find "$arg2" | sed 's% %#%g'`
do
file=`echo $file1 | sed 's%#% %g'`
file_relative_name=${file#$arg2/}
file "$file" | grep -Eqv "directory"
if [ $? -ne 0 ];then
continue
fi
if [ ! -f "$arg1/$file_relative_name" ];then
echo "$file_relative_name" >> $tmp_dir/B_only_file
fi
done
#############################################################
# 根据输入标签打开用vim打开文件比较diff
#############################################################
if [[ ! -f $tmp_dir/diff_file && ! -f $tmp_dir/A_only_file && ! -f $tmp_dir/B_only_file ]];then
echo folders are the same!
myexit
fi
show_diff $tmp_dir/diff_file
while true
do
if [ -f $tmp_dir/diff_file ];then
echo -n "Please choose file number list (like this:1,3-4,5):"
else
echo "No diff files,enter 'q' to exit!"
echo -n ":"
fi
read value
if [[ "$value" = "s" ]] || [[ "$value" = "S" ]];then
show_diff $tmp_dir/diff_file
continue
elif [[ "$value" = "q" ]] || [[ "$value" = "Q" ]];then
myexit
elif [[ "$value" = "a" ]] || [ "$value" = "A" ];then
value="1-$"
fi
vim_script=`check_value $tmp_dir/diff_file "$value" 2>/dev/null`
if [ $? -ne 0 ];then
echo "invalid parameter[$value]!"
else
vim -c "$vim_script"
fi
done