Created
June 27, 2022 18:12
-
-
Save docwhat/3a726d0db51e32ef8c5e2db4bf8d294a to your computer and use it in GitHub Desktop.
Ways of checking if a file is "a symlink" and is "broken".
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
#!/bin/sh | |
set -eu | |
tempdir='' | |
setup() { | |
tempdir="$(mktemp -d)" | |
cd "$tempdir" | |
touch target | |
ln -nsf target good | |
ln -nsf nosuch bad | |
ln -nsf good goodgood | |
ln -nsf bad badgood | |
} | |
teardown() { | |
if [ -d "$tempdir" ]; then | |
rm -rf "$tempdir" | |
fi | |
} | |
check() { | |
printf "%s is " "${1}" | |
eval "tput bold; if ${1}; then tput setaf 6; echo true; else tput setaf 1; echo false; fi; tput sgr0" | |
} | |
run() { | |
echo POSIX | |
for f in *; do | |
check "[ -h $f ]" | |
check "[ -L $f ]" | |
check "[ -e $f ]" | |
check "[ -f $f ]" | |
done | |
echo NON-POSIX | |
for f in *; do | |
check "[[ -h $f ]]" | |
check "[[ -L $f ]]" | |
check "[[ -a $f ]]" | |
check "[[ -e $f ]]" | |
check "[[ -f $f ]]" | |
done | |
echo OTHER TOOLS | |
for f in *; do | |
# check "find $f -type l \! -exec test -e {} \; -print" | |
check "[ -z \"\$(find -L $f -type l)\" ]" | |
check "[ -z \"\$(gfind -L $f -type l)\" ]" | |
done | |
} | |
## Main ## | |
setup | |
trap teardown EXIT | |
run | |
## EOF ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment