-
Notifications
You must be signed in to change notification settings - Fork 0
/
diffcp
executable file
·92 lines (68 loc) · 1.26 KB
/
diffcp
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
#!/bin/bash
usage() {
cat >&2 <<EOM
usage: $(basename "$0") SOURCE DEST
Show a diff of source and dest, and prompt before overwriting.
options:
-h show this help message
-m use mv rather than cp
-q be more quiet, less verbose
-b make a backup of the destination file
EOM
}
operation=cp
verbose=1
backup=
while getopts bhmv OPT; do
case $OPT in
b) backup=1 ;;
m) operation=mv ;;
q) verbose='' ;;
h)
usage
exit 0
;;
\?)
usage
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ -z "$1" -o -z "$2" ]; then
usage
exit 1
fi
set -o nounset
src="$1"
dst="$2"
diff="diff"
if which colordiff > /dev/null; then
diff="colordiff"
fi
"$diff" -u -- "$src" "$dst"
ret=$?
if [ $ret -eq 0 ]; then
# no changes
if [ -n "$verbose" ]; then
echo >&2 "\`$src' == \`$dst'"
fi
exit 0
elif [ $ret -eq 1 ]; then
# changes
opts=()
if [ -n "$verbose" ]; then
opts+=("-v")
fi
if [ -n "$backup" ]; then
opts+=("-b")
fi
if [ "$operation" = "cp" ]; then
opts+=("-a")
fi
echo
"$operation" -i "${opts[@]}" "$src" "$dst"
else
# error
exit $ret
fi