move-if-change 2.61 KB
Newer Older
1
#!/bin/sh
2 3 4
# Like mv $1 $2, but if the files are the same, just delete $1.
# Status is zero if successful, nonzero otherwise.

Paul Eggert's avatar
Paul Eggert committed
5
VERSION='2018-03-07 03:47'; # UTC
6 7 8 9 10
# The definition above must lie within the first 8 lines in order
# for the Emacs time-stamp write hook (at end) to update it.
# If you change this file with Emacs, please let the write hook
# do its job.  Otherwise, update this string manually.

Paul Eggert's avatar
Paul Eggert committed
11
# Copyright (C) 2002-2019 Free Software Foundation, Inc.
12 13 14 15 16 17 18 19 20 21 22 23

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
Paul Eggert's avatar
Paul Eggert committed
24
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
25 26 27 28 29 30 31 32 33 34

usage="usage: $0 SOURCE DEST"

help="$usage
  or:  $0 OPTION
If SOURCE is different than DEST, then move it to DEST; else remove SOURCE.

  --help     display this help and exit
  --version  output version information and exit

Paul Eggert's avatar
Paul Eggert committed
35
The variable CMPPROG can be used to specify an alternative to 'cmp'.
36

37 38
Report bugs to <bug-gnulib@gnu.org>."

Paul Eggert's avatar
Paul Eggert committed
39
year=`expr "$VERSION" : '\([^-]*\)'`
40 41
version=`expr "$VERSION" : '\([^ ]*\)'`
version="move-if-change (gnulib) $version
Paul Eggert's avatar
Paul Eggert committed
42
Copyright $year Free Software Foundation, Inc.
Paul Eggert's avatar
Paul Eggert committed
43
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>
44 45 46
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."

47
cmpprog=${CMPPROG-cmp}
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

for arg
do
  case $arg in
    --help | --hel | --he | --h)
      exec echo "$help" ;;
    --version | --versio | --versi | --vers | --ver | --ve | --v)
      exec echo "$version" ;;
    --)
      shift
      break ;;
    -*)
      echo "$0: invalid option: $arg" >&2
      exit 1 ;;
    *)
      break ;;
  esac
done

67
test $# -eq 2 || { echo "$0: $usage" >&2; exit 1; }
68

69
if test -r "$2" && $cmpprog -- "$1" "$2" >/dev/null; then
70
  rm -f -- "$1"
71
else
72 73 74 75
  if mv -f -- "$1" "$2"; then :; else
    # Ignore failure due to a concurrent move-if-change.
    test -r "$2" && $cmpprog -- "$1" "$2" >/dev/null && rm -f -- "$1"
  fi
76
fi
77 78

## Local Variables:
Paul Eggert's avatar
Paul Eggert committed
79
## eval: (add-hook 'before-save-hook 'time-stamp)
80 81
## time-stamp-start: "VERSION='"
## time-stamp-format: "%:y-%02m-%02d %02H:%02M"
Paul Eggert's avatar
Paul Eggert committed
82
## time-stamp-time-zone: "UTC0"
83 84
## time-stamp-end: "'; # UTC"
## End: