-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpifdiff
executable file
·74 lines (64 loc) · 1.64 KB
/
cpifdiff
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
#!/bin/sh
#=======================================================================
# $Id: cpifdiff,v 1.4 2017/03/01 14:17:12 chah Exp $
# Copy ifile (or standard input if omitted) to ofile if they differ
#=======================================================================
progname=`basename $0`
usage() {
echo "Usage: $progname [-l logmsg] [ifile] ofile" >&2
exit 2
}
tmp=`mktemp -t cpifdiff.XXXXX`
trap 'trap 0; rm -f $tmp; exit 1' 1 2 15
fail () {
echo $progname: "$@" >&2
rm -f $tmp
exit 1
}
opt_log=n
while getopts "l:" opt; do
case $opt in
l)
opt_log=y
logmsg=$OPTARG
;;
*)
usage
;;
esac
done
shift `expr $OPTIND - 1`
test $# -ge 1 -a $# -le 2 || usage
#--- '-' as first argument means standard input
test $# -eq 2 -a "$1" = "-" && shift
#--- if standard input, copy to temporary file
if [ $# -eq 1 ]; then
cat > $tmp || fail "Failed to copy standard input"
ifile=$tmp
ofile=$1
else
ifile=$1
ofile=$2
fi
#--- Test input file access
test -f "$ifile" || fail "$ifile: No such file"
test -r "$ifile" || fail "$ifile: Permission denied"
#--- If the output file does not exist, create it
if [ ! -f "$ofile" ]; then
touch "$ofile" || fail "$ofile: Permission denied"
cp $ifile $ofile || fail "Failed writing $ofile"
if [ "$opt_log" = "y" ]; then
echo "$logmsg"
fi
#--- Otherwise, compare with existing file
else
test -r "$ofile" -a -w "$ofile" || fail "$ofile: Permission denied"
if not cmp -s "$ifile" "$ofile"; then
cp "$ifile" "$ofile" || fail "Failed writing $ofile"
if [ "$opt_log" = "y" ]; then
echo "$logmsg"
fi
fi
fi
rm -f $tmp
exit 0