问题
What's the best portable (POSIX?) way to script finding the target for a link if readlink
and realpath
are not available?
Would you ls -l
, and if it starts with l
take the text after the ->
with sed
and repeat until it no longer starts with l
?
回答1:
Per BashFAQ #29 (which also endorses the GNU find approach suggested by @EugeniuRosca):
One widely available (though not pure-POSIX) option is to use perl
:
target=/path/to/symlink-name perl -le 'print readlink $ENV{target}'
If your symlink's name is guaranteed not to contain ->
, you can parse the output of ls
.
The below code 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" ||:
}
回答2:
LIMITATION: -printf
is not a POSIX-specified option
#!/bin/bash
tmp=<symlink-name>
tmp1=''
while tmp=$(find "$tmp" -prune -printf "%l" 2>/dev/null); do
target="$tmp1" && tmp1="$tmp"
done;
echo "$target"
回答3:
Here is another solution that is very similar to Charles Duffy's. I'm not that experienced so there may be some non-POSIX or performance issues. I arrived at this after looking at Charles' solution and replacing anything I didn't understand :-P It's very possible that after any issues here are fixed, you end up with Charles' solution all over 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.
来源:https://stackoverflow.com/questions/31596363/how-to-recursively-resolve-symlinks-without-readlink-or-realpath