795
Comment: POSIX too
|
799
don't use [ -a ], since Posix leaves the behaviour of test unspecified when there are more than four arguments
|
Deletions are marked like this. | Additions are marked like this. |
Line 18: | Line 18: |
if [ -L "$symlink" -a ! -e "$symlink" ]; then | if [ -L "$symlink" ] && [ ! -e "$symlink" ]; then |
How do I determine whether a symlink is dangling (broken)?
The documentation on this is fuzzy, but it turns out you can do this with shell builtins:
# Bash if [[ -L $symlink && ! -e $symlink ]]; then echo "$symlink is dangling" fi
The -e test (like all other tests besides -L or -h) follows the symbolic link, and therefore it checks on the thing pointed to, not on the link itself. The -L test does not follow the symlink, so it's checking on the link itself. Together, they can indicate the presence of a dangling symlink.
POSIX has these same tests, with the same semantics, so it can also be done with the [ command:
# POSIX if [ -L "$symlink" ] && [ ! -e "$symlink" ]; then echo "$symlink is dangling" fi