move-if-change revision 1.3 1 1.1 christos #!/bin/sh
2 1.1 christos # Like mv $1 $2, but if the files are the same, just delete $1.
3 1.1 christos # Status is zero if successful, nonzero otherwise.
4 1.1 christos
5 1.3 christos VERSION='2012-01-06 07:23'; # UTC
6 1.1 christos # The definition above must lie within the first 8 lines in order
7 1.1 christos # for the Emacs time-stamp write hook (at end) to update it.
8 1.1 christos # If you change this file with Emacs, please let the write hook
9 1.1 christos # do its job. Otherwise, update this string manually.
10 1.1 christos
11 1.3 christos # Copyright (C) 2002-2014 Free Software Foundation, Inc.
12 1.1 christos
13 1.1 christos # This program is free software: you can redistribute it and/or modify
14 1.1 christos # it under the terms of the GNU General Public License as published by
15 1.1 christos # the Free Software Foundation, either version 3 of the License, or
16 1.1 christos # (at your option) any later version.
17 1.1 christos
18 1.1 christos # This program is distributed in the hope that it will be useful,
19 1.1 christos # but WITHOUT ANY WARRANTY; without even the implied warranty of
20 1.1 christos # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 1.1 christos # GNU General Public License for more details.
22 1.1 christos
23 1.1 christos # You should have received a copy of the GNU General Public License
24 1.1 christos # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 1.1 christos
26 1.1 christos usage="usage: $0 SOURCE DEST"
27 1.1 christos
28 1.1 christos help="$usage
29 1.1 christos or: $0 OPTION
30 1.1 christos If SOURCE is different than DEST, then move it to DEST; else remove SOURCE.
31 1.1 christos
32 1.1 christos --help display this help and exit
33 1.1 christos --version output version information and exit
34 1.1 christos
35 1.3 christos The variable CMPPROG can be used to specify an alternative to 'cmp'.
36 1.1 christos
37 1.1 christos Report bugs to <bug-gnulib (at] gnu.org>."
38 1.1 christos
39 1.1 christos version=`expr "$VERSION" : '\([^ ]*\)'`
40 1.1 christos version="move-if-change (gnulib) $version
41 1.1 christos Copyright (C) 2011 Free Software Foundation, Inc.
42 1.1 christos License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
43 1.1 christos This is free software: you are free to change and redistribute it.
44 1.1 christos There is NO WARRANTY, to the extent permitted by law."
45 1.1 christos
46 1.1 christos cmpprog=${CMPPROG-cmp}
47 1.1 christos
48 1.1 christos for arg
49 1.1 christos do
50 1.1 christos case $arg in
51 1.1 christos --help | --hel | --he | --h)
52 1.1 christos exec echo "$help" ;;
53 1.1 christos --version | --versio | --versi | --vers | --ver | --ve | --v)
54 1.1 christos exec echo "$version" ;;
55 1.1 christos --)
56 1.1 christos shift
57 1.1 christos break ;;
58 1.1 christos -*)
59 1.1 christos echo "$0: invalid option: $arg" >&2
60 1.1 christos exit 1 ;;
61 1.1 christos *)
62 1.1 christos break ;;
63 1.1 christos esac
64 1.1 christos done
65 1.1 christos
66 1.1 christos test $# -eq 2 || { echo "$0: $usage" >&2; exit 1; }
67 1.1 christos
68 1.1 christos if test -r "$2" && $cmpprog -- "$1" "$2" >/dev/null; then
69 1.1 christos rm -f -- "$1"
70 1.1 christos else
71 1.1 christos if mv -f -- "$1" "$2"; then :; else
72 1.1 christos # Ignore failure due to a concurrent move-if-change.
73 1.1 christos test -r "$2" && $cmpprog -- "$1" "$2" >/dev/null && rm -f -- "$1"
74 1.1 christos fi
75 1.1 christos fi
76 1.1 christos
77 1.1 christos ## Local Variables:
78 1.1 christos ## eval: (add-hook 'write-file-hooks 'time-stamp)
79 1.1 christos ## time-stamp-start: "VERSION='"
80 1.1 christos ## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
81 1.1 christos ## time-stamp-time-zone: "UTC"
82 1.1 christos ## time-stamp-end: "'; # UTC"
83 1.1 christos ## End:
84