I am creating a bash function that should return true/false if a specified symlink refers to specified target. I based myself on https://unix.stackexchange.com/a/192341/40237
However, I am having trouble getting readlink
to work as I want:
is_symlink_to () {
# $1 = symlink / $2 = symlink target
echo "arg1: $1 and arg2: $2"
echo readlink arg 1 is: $(readlink -v $1 ) # -v for troubleshooting
if [ "$(readlink -- $1)" = $2 ]; then
echo "$1 is a symlink to $2"
return 0;
else
return 1;
fi
}
...
if is_symlink_to "~/$file" "$dir/$file" ; then
echo "is already symlinked"
else
...
fi
Question: Why does readlink -v
return No such file or directory
?
arg1: ~/.bash_profile and arg2: /home/me/dotfiles/.bash_profile
readlink: '~/.bash_profile': No such file or directory
readlink arg 1 is:
If I run readlink
from a bash shell, it works fine:
me@mango:~/dotfiles$ readlink -v ~/.bash_profile
/home/me/dotfiles/.bash_profile
is_symlink_to
, and try if insteadis_symlink_to ~/"$file" "$dir/$file"
works. – AdminBee Nov 11 '19 at 11:07[ "$file1" -ef "$file2" ]
to check whether two paths refer to the same file (after symlink resolution) – Stéphane Chazelas Nov 11 '19 at 12:24~
) and use--
to mark the end of options forreadlink
(readlink -v -- "$file"
) – Stéphane Chazelas Nov 11 '19 at 12:26