Last active
October 7, 2015 14:48
-
-
Save np/3181899 to your computer and use it in GitHub Desktop.
link
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# link() @ https://gist.github.com/np/3181899#file-link-sh {{{ | |
# Dependencies: | |
# error() @ https://gist.github.com/np/3736727#file-error-sh | |
# Examples: | |
# cd ~/configs | |
# link .zshrc ~/.zshrc | |
# link .vimrc ~/.vimrc | |
link(){ | |
local dst="$1" | |
local ldst="$1" | |
local src="$2" | |
case "$dst" in | |
/*) : ;; | |
*) ldst="$(realpath "$dst" --relative-to="$(dirname "$2")")";; | |
esac | |
if [ -L "$src" ]; then | |
# Check if the link is already as expected. | |
[ $(readlink "$src") != "$ldst" ] || return 0 | |
rm "$src" | |
elif [ -e "$src" ]; then | |
if [ -e "$dst" ]; then | |
error 1 "$src already exists, fix this and relaunch" | |
else | |
echo "moving $src to $dst" >>/dev/stderr | |
mv "$src" "$dst" | |
fi | |
elif [ ! -e "$dst" ]; then | |
# if nothing exists we do nothing | |
return 0 | |
fi | |
echo "linking $dst" >>/dev/stderr | |
ln -s "$ldst" "$src" | |
} | |
# }}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment