8 symlinks-analyze - Discover where symlinks point at, recursively
17 .
/usr
/lib
/tool
/bash-utils
26 if [ "$x" = / ]; then echo /; return; fi
32 local path
=$1 logical_path
=$2
35 if [ "${path:0:1}" = / ]
39 echo -n " [$logical_path]"
48 local realpath
=`readlink -n -f "$path"`
49 if [ "$realpath" != "$path" ]
51 echo -n " ($realpath)"
59 local dir
=$
(dirname "$path")
60 cd "$dir" && pwd ||
kill -ABRT $$
61 # kill ourself to prevent bad data at the caller,
62 # so the caller does not need to set errexit
68 local dirname=`logical_dirname "$path"`
69 if [ "$dirname" != / ]; then dirname=$dirname/; fi
70 local basename=$
(basename "$path")
71 echo "$dirname$basename"
86 echo "Usage: symlinks-analyze [OPTIONS] <PATH> [<PATH> [...]]"
88 echo " -a, --absolute-path shown in [ brackets ]"
89 echo " -r, --real-path shown in ( brackets )"
90 echo " -p, --parents analyze parent directories of any traversed path too"
91 echo " -s, --skip-non-symlinks"
93 -a|
--abs|
--absolute|
--abs-path|
--absolute-path)
95 -p|
--parent|
--parents)
97 -r|
--realpath|
--real-path)
99 -s|
--skip-non-symlinks)
100 skip_non_symlinks
=1;;
101 -*) echo "$0: unknown option: $1" >&2
109 declare -a paths_to_analyze
=("$@")
110 declare -a paths_analyzed
=()
113 while [ ${#paths_to_analyze[@]} -gt 0 ]
115 declare -a visited
=()
116 show_this_analysis
=yes
118 path_to_analyze
=${paths_to_analyze[0]}
119 path
=`notrailingslash "$path_to_analyze"`
121 if [ $skip_non_symlinks ]
125 show_this_analysis
=no
129 if [ ! -e "$path" -a ! -L "$path" ]
131 # neither an actual existing file nor a broken symlink
132 warnx
"$path: not found"
133 show_this_analysis
=no
136 if [ $show_this_analysis = yes ]
140 logical_path
=`logical_path "$path"`
141 show_abs_path
"$path" "$logical_path"
142 show_realpath
"$path"
143 visited
+=("$logical_path")
149 parent
=`logical_dirname "$path"`
150 if [ "$parent" != / ]
152 if ! in_list
"$parent" "${paths_to_analyze[@]}" "${paths_analyzed[@]}"
154 paths_to_analyze
+=("$parent")
165 target
=`readlink "$path"`
166 dir
=`dirname "$path"`
170 echo -n " -> $target"
171 logical_path
=`logical_path "$target"`
172 show_abs_path
"$target" "$logical_path"
174 if in_list
"$logical_path" "${visited[@]}"
178 visited
+=("$logical_path")
181 path
=`notrailingslash "$path"`
191 array_shift paths_to_analyze
>/dev
/null
192 paths_analyzed
+=("$path_to_analyze")