如何知道 Linux 中符号链接的级别?

如何知道 Linux 中符号链接的级别?

例如,如果符号链接

a -> b
b -> c
c -> d

假设符号链接a的级别是3。

那么,有什么实用程序可以获取此信息吗?而且,我还想获取符号链接的扩展详细信息,它将向我显示以下内容:

1. /abc/xyz is expanded to /abc/xy/z (lrwx--x--x root root)
2. /abc/xy/z is expanded to /abc/xy-1.3.2/z (lrwx--x--x root root)
3. /abc/xy-1.3.2/z is expanded to /abc/xy-1.3.2/z-4.6 (lrwx--x--x root root)
4. /abc/xy-1.3.2/z-4.6 is expanded to /storage/121/43/z_4_6 (lrwx--x--x root root)
5. /storage/121/43/z_4_6 is expanded to /media/kitty_3135/43/z_4_6 (lrwx--x--x root root)

所以我可以使用符号链接进行诊断。有什么想法吗?

答案1

这个递归 Bash 函数将打印链接链和计数以及诊断:

chain() { local link target; if [[ -z $_chain ]]; then unset _chain_count _expansion; _chain="$1"; fi; link=$(stat --printf=%N $1); while [[ $link =~ \-\> ]]; do target="${link##*\`}"; target="${target%\'}"; _chain+=" -> $target"; ((_chain_count++)); _expansion+="$_chain_count. $1 is expanded to $target $(stat --printf="(%A %U %G)" $target)"$'\n'; chain "$target"; return; done; echo "$_chain ($_chain_count)"; echo "$_expansion"; unset _chain _chain_count _expansion; }

它需要。有关更多信息和使用而不是stat的版本,请参阅我的回答readlinkstat这里(需要添加计数功能,但添加权限和所有者/组会更具挑战性)。

为了这:

a
b -> a
c -> b
d -> c

输出chain d将是:

d -> c -> b -> a (3)
1. d is expanded to c (lrwxrwxrwx username groupname)
2. c is expanded to b (lrwxrwxrwx username groupname)
3. b is expanded to a (-rw-r--r-- root root)

以下是该函数的更易读的版本:

chain ()
{
    local link target;
    if [[ -z $_chain ]]; then
        unset _chain_count _expansion;
        _chain="$1";
    fi;
    link=$(stat --printf=%N $1);
    while [[ $link =~ \-\> ]]; do
        target="${link##*\`}";
        target="${target%\'}";
        _chain+=" -> $target";
        ((_chain_count++));
        _expansion+="$_chain_count. $1 is expanded to $target $(stat --printf="(%A %U %G)" $target)"$'\n';
        chain "$target";
        return;
    done;
    echo "$_chain ($_chain_count)";
    echo "$_expansion";
    unset _chain _chain_count _expansion
}

答案2

总的来说,这实际上相当棘手,因为您可能拥有如下符号链接:

ln -s ../symlink/xyz pqr

其中“symlink”本身是任意长度的符号链接。我有一个程序可以完成与realpath()但它也会检查途中所有符号链接的安全性。因此,它可以用来回答您的问题。它的一些测试脚本可以用来验证您的计算。

这是一个这样的测试:

#!/bin/ksh
#
# @(#)$Id: realpathtest.sh,v 1.4 2008/04/14 19:36:54 jleffler Exp $
#
# Create confusing path names

base=${1:-"./rpt"}

mkdir -p $base/elsewhere/confused $base/elsewhere/hidden \
         $base/other/place $base/elsewhere/private

(
cd $base
[ -h link ] || ln -s elsewhere/confused link
[ -h some ] || ln -s other/place some
)
(
cd $base/elsewhere/confused
[ -h mischief ] || ln -s ../hidden mischief
[ -h where    ] || ln -s ../private where
)
(
cd $base/other/place
[ -h dubious  ] || ln -s ../../link/mischief dubious
[ -h doubtful ] || ln -s ../../link/where doubtful
)
(
cd $base/elsewhere/private
echo "What is the real pathname for $base/some/doubtful/file?" > file
)
(
cd $base/elsewhere/hidden
[ -h file ] || ln -s name file
echo "What is the real pathname for $base/some/dubious/file?" > name
)

for name in \
    $base/some/dubious/file \
    $base/some/doubtful/file \
    $base/elsewhere/confused/mischief/file \
    $base/elsewhere/confused/where/file
do
    if realpath $name
    then cat $(realpath -s $name)
    fi
done

如果您需要“realpath”或“linkpath”程序的源代码,请联系我(请参阅我的个人资料);。“realpath”程序并不是什么深奥的科学 - 它基本上会调用realpath()给定的每个参数。分析完整的链接集需要付出更多的努力。

答案3

我对实用程序不太了解。您可以很容易地编写脚本。添加一些 stat 或 ls 输出可能会让您完成其余的工作。

#!/bin/sh

level=0
file=$1
while [ -h "$file" ]
do
    previous="$file"
    file=`readlink "$file"`
    level=$(($level+1))
    echo "$level". "$previous -> $file"
done

在我的主目录中设置的几个链接上使用它,我得到:

$ ./test.sh link2
1. link2 -> link1
2. link1 -> Test.java

相关内容