I\'m trying to check if a symlink exists in bash. Here\'s what I\'ve tried.
mda=/usr/mda
if [ ! -L $mda ]; then
echo \"=> File doesn\'t exist\"
fi
mda
Is the file really a symbolic link? If not, the usual test for existence is -r
or -e
.
See man test
.
first you can do with this style:
mda="/usr/mda"
if [ ! -L "${mda}" ]; then
echo "=> File doesn't exist"
fi
if you want to do it in more advanced style you can write it like below:
#!/bin/bash
mda="$1"
if [ -e "$1" ]; then
if [ ! -L "$1" ]
then
echo "you entry is not symlink"
else
echo "your entry is symlink"
fi
else
echo "=> File doesn't exist"
fi
the result of above is like:
root@linux:~# ./sym.sh /etc/passwd
you entry is not symlink
root@linux:~# ./sym.sh /usr/mda
your entry is symlink
root@linux:~# ./sym.sh
=> File doesn't exist