In case $target_path
does not match the link text exactly, you can check that they are, in fact equivalent (regardless of name). But since a hardlink is preferable you might want to check that case, too (see below).
A more generic solution is:
[ "$(readlink $HOME/.slate.js)" -ef "$target_path" ]
Or, as in your example:
target_path=$HOME/Code/slate/.slate.js
if [ "`readlink $HOME/.slate.js`" -ef "$target_path" ]; then
rm -rf "$HOME/.slate.js"
fi
But that all assumes that your $HOME/.slate.js
is a symbolic link. If it is a hard link (which is preferable, when possible), then it is simpler:
… [ "$HOME/.slate.js" -ef "$target_path" ] …
Maybe something like (check whether it is a symlink, if so, then check that link matches target; otherwise check whether the files same—either a hard link or actually the same file):
… [ \( -L "$HOME/.slate.js" -a "`readlink $HOME/.slate.js`" -ef "$target_path" \) \
-o \( "$HOME/.slate.js" -ef "$target_path" \) ] …
You should also check whether the file is, in fact, the same file (and not a hard link), otherwise you will delete the one and only copy.
$HOME
have a space in it, perchance? – Dwinnelltest
is probably not a good name for a shell script, as it's already a shell built-in and a binary in/bin
(or/usr/bin
)... – Pyrometallurgy