Skip to content

Instantly share code, notes, and snippets.

@nlindley
Created September 13, 2013 21:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlindley/6556392 to your computer and use it in GitHub Desktop.
Save nlindley/6556392 to your computer and use it in GitHub Desktop.
Swaps two files.
#!/bin/sh
if test $# -ne 2
then
echo "Usage: swap <filename> <filename>"
exit 1
fi
if test ! -w "$1"
then
echo "$1 is not writable"
exit 2
fi
if test ! -w "$2"
then
echo "$2 is not writable"
exit 2
fi
tempfoo=`basename $0`
TEMPFILE=`mktemp -t ${tempfoo}`
if [ $? -ne 0 ]
then
echo "Could not create temporary file"
exit 3
fi
mv "$1" "$TEMPFILE"
mv "$2" "$1"
mv "$TEMPFILE" "$2"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment