39 lines
949 B
Bash
Executable File
39 lines
949 B
Bash
Executable File
#!/bin/sh
|
|
|
|
usage () {
|
|
cat <<EOF>&2
|
|
Usage: ${0##*/} FILE1 FILE2
|
|
|
|
Swap 2 filenames around.
|
|
|
|
EOF
|
|
}
|
|
|
|
[ $# -ne 2 ] && usage && exit 1
|
|
[ "$1" = "-h" ] && usage && exit
|
|
[ "$1" = "--" ] && shift
|
|
|
|
## We need a temp file is case both files are in the same folder. We use the
|
|
## unsecure option since we do not want to create an empty file in
|
|
## advance. Indeed, creating a file with mktemp is problematic for folders since
|
|
## non-GNU mv can only move _into_ an existing folder. But we do not overwrite
|
|
## if file exists.
|
|
TEMPFILE="$(mktemp -u "$1.XXXXXX")"
|
|
|
|
case "$1" in
|
|
*/*) DIR1="${1%/*}" ;;
|
|
*) DIR1="." ;;
|
|
esac
|
|
case "$2" in
|
|
*/*) DIR2="${2%/*}" ;;
|
|
*) DIR2="." ;;
|
|
esac
|
|
|
|
if ! mv -n "$1" "$TEMPFILE"; then
|
|
echo >&2 "Cannot move to temporary file '$TEMPFILE' (maybe try again)"
|
|
fi
|
|
## We need to specify the directories because of non-uniform behaviour across
|
|
## the various 'mv' implementations.
|
|
mv -n "$2" "$DIR2/${1##*/}"
|
|
mv -n "$TEMPFILE" "$DIR1/${2##*/}"
|