move-if-change 2.6 KB

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