Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Validates link given by path returns 1 if it's invalid.
- # Invalid link's absolute paths will also be forwarded to stdout.
- # first parameter must be path
- # after path, flags can follow:
- # -r --remove remove invalid link immediatelly
- # -a --all print given absolute link path on the stdout even if it's valid.
- validatelink() {
- # dbg "Param \$0: $0"
- # dbg "Param \$1: $1"
- # dbg "Param \$2: $2"
- #while read line ; do
- # echo "LINK: $line";
- #
- #; done
- linkpath=$(abspath $1);
- shift;
- REMOVE=false;
- ALL=false;
- # This while finds any -flags that can be set
- while [[ $# > 0 ]]
- do
- key="$1"
- # dbg "Checking param: \"$key\"";
- case $key in
- -r|--remove)
- REMOVE=true;
- # dbg " Remove enabled";
- ;;
- -a|--all)
- ALL=true;
- ;;
- *)
- # dbg " WARNING: Unknown parameter \"$key\"";
- ;;
- esac
- shift
- done
- # Path will now contain link target path
- dbg "LINK: $linkpath";
- path=$(readlink -n -s -f $linkpath);
- # -f checks if file exists
- if ! [ -f $path ] && ! [ -d $path ]; then
- echo "$linkpath"
- dbg " => File $path doesn't exist"
- if [ "$REMOVE" = true ]; then
- dbg " - Removing this nasty file."
- rm "$linkpath"
- fi
- return 1;
- else
- if [ "$ALL" = true ]; then
- dbg "Print valid link: " --nonewline
- echo "$linkpath"
- else
- dbg " => File: $path"
- fi
- return 0;
- fi
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement