Mat*_*Mat 97
您可以使用该readlink实用程序,并提供以下-f选项:
-f, --canonicalizeRun Code Online (Sandbox Code Playgroud)canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist
一些发行版,例如那些使用GNU coreutils和FreeBSD 的发行版,还附带了一个realpath(1)实用程序,它基本上只是调用realpath(3)并执行几乎相同的事情。
Gil*_*il' 25
可移植地,该PWD变量由外壳程序设置为当前目录的一个绝对位置。该路径的任何组件都可以是符号链接。
case $f in
/*) absolute=$f;;
*) absolute=$PWD/$f;;
esac
Run Code Online (Sandbox Code Playgroud)
如果你想消除.和..也,切换到该目录包含的文件,并获得$PWD有:
if [ -d "$f" ]; then f=$f/.; fi
absolute=$(cd "$(dirname -- "$f")"; printf %s. "$PWD")
absolute=${absolute%?}
absolute=$absolute/${f##*/}
Run Code Online (Sandbox Code Playgroud)
没有可移植的方式来跟踪符号链接。如果你有一个目录的路径,那么在大多数 unices 上$(cd -- "$dir" && pwd -P 2>/dev/null || pwd)提供了一个不使用符号链接的路径,因为跟踪符号链接的 shell 倾向于实现pwd -P(“P”代表“物理”)。
一些 unice 提供了一个实用程序来打印文件的“物理”路径。
readlink -f,FreeBSD ?8.3、NetBSD ?4.0 和 OpenBSD 早在 2.2 也是如此。realpath(它也存在于一些 Linux 系统上,并且在 BusyBox 中)。如果 Perl 可用,您可以使用Cwd模块.
perl -MCwd -e 'print Cwd::realpath($ARGV[0])' path/to/file
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
84756 次 |
| 最近记录: |