How to recursively resolve symbolic links without readlink or realpath?
Per BashFAQ # 29 (which also supports the GNU search approach suggested by @EugeniuRosca ):
One widely available (although not pure-POSIX) option is to use perl
:
target=/path/to/symlink-name perl -le 'print readlink $ENV{target}'
If your symlink name will not contain ->
, you can parse the output ls
.
The code below combines both approaches:
# define the best readlink function available for this platform
if command -v readlink >/dev/null 2>/dev/null; then
# first choice: Use the real readlink command
readlink() {
command readlink -- "$@"
}
elif find . -maxdepth 0 -printf '%l' >/dev/null 2>/dev/null; then
# second choice: use GNU find
readlink() {
local ll candidate >/dev/null 2>&1 ||:
if candidate=$(find "$1" -maxdepth 0 -printf '%l') && [ "$candidate" ]; then
printf '%s\n' "$candidate"
else
printf '%s\n' "$1"
fi
}
elif command -v perl >/dev/null 2>/dev/null; then
# third choice: use perl
readlink() {
local candidate ||:
candidate=$(target=$1 perl -le 'print readlink $ENV{target}')
if [ "$candidate" ]; then
printf '%s\n' "$candidate"
else
printf '%s\n' "$1"
fi
}
else
# fourth choice: parse ls -ld
readlink() {
local ll candidate >/dev/null 2>&1 ||:
ll=$(LC_ALL=C ls -ld -- "$1" 2>/dev/null)
candidate=${ll#* -> }
if [ "$candidate" = "$ll" ]; then
printf '%s\n' "$1"
else
printf '%s\n' "$candidate"
fi
}
fi
readlink_recursive() {
local path prev_path oldwd found_recursion >/dev/null 2>&1 ||:
oldwd=$PWD; path=$1; found_recursion=0
while [ -L "$path" ] && [ "$found_recursion" = 0 ]; do
if [ "$path" != "${path%/*}" ]; then
cd -- "${path%/*}" || {
cd -- "$oldwd" ||:
echo "ERROR: Directory '${path%/*}' does not exist in '$PWD'" >&2
return 1
}
path=${PWD}/${path##*/}
fi
path=$(readlink "$path")
if [ -d "$path" ]; then
cd -- "$path"
path=$PWD
break
fi
if [ "$path" != "${path%/*}" ]; then
cd -- "${path%/*}" || {
echo "ERROR: Could not traverse from $PWD to ${path%/*}" >&2
return 1
}
path=${PWD}/${path##*/}
elif [ "$PWD" != "$oldwd" ]; then
path=${PWD}/$path
fi
for prev_path; do
if [ "$path" = "$prev_path" ]; then
found_recursion=1
break
fi
done
set -- "$path" "$@" # record path for recursion check
done
if [ "$path" != "${path%/../*}" ]; then
cd "${path%/*}" || {
echo "ERROR: Directory '${path%/*}' does not exist in $PWD" >&2
return 1
}
printf '%s\n' "$PWD/${path##*/}"
else
printf '%s\n' "$path"
fi
cd -- "$oldwd" ||:
}
source to share
Here is another solution very similar to Charles Duffy. I'm not that experienced so there might be non-POSIX or performance issues. I approached this by looking at Charles's solution and replacing anything I didn't understand: - It is very possible that after you fix any problems, you will get Charles's solution again.
resolve() {
local arg path absolute ll dir prev_path oldwd found_recursion base >/dev/null 2>&1 ||:
arg="$1"; path="$1"; oldwd=$PWD; found_recursion=0
dir=$(dirname "$path")
cd -- "$dir" || {
cd -- "$oldwd" ||:
echo "While resolving '$arg' could not go to '$dir'" >&2
return 1
}
if [ $PWD = "/" ]; then
absolute="/$(basename $path)"
else
absolute="$PWD/$(basename $path)"
fi
[ "$path" != "$absolute" ] && set -- "$absolute"
while [ -L "$absolute" ] && [ "$found_recursion" = 0 ]; do
ll=$(LC_ALL=C \ls -ld -- "$absolute" 2>/dev/null)
path=${ll#* -> }
dir=$(dirname "$path")
cd -- "$dir" || {
cd -- "$oldwd" ||:
echo "While resolving '$arg' could not go to '$dir'" >&2
return 1
}
base=$(basename "$path")
absolute="$PWD/$base"
for prev_path; do
if [ "$absolute" = "$prev_path" ]; then
found_recursion=1
break
fi
done
set -- "$absolute" "$@"
done
if [ -d "$absolute" ]; then
cd -- "$absolute" || {
cd -- "$oldwd" ||:
echo "While resolving '$arg' could not go to '$absolute'" >&2
return 1
}
printf '%s\n' "$PWD"
else
printf '%s\n' "$absolute"
fi
}
Edit: now using $PWD
and printf
, canonicalize result if directory.
source to share