作为我脚本的参数,有一些文件路径。当然,这些可以是相对的(或包含 ~)。但是对于我编写的函数,我需要绝对路径,但不解析它们的符号链接(symbolic link)。
这个有什么功能吗?
最佳答案
MY_PATH=$(readlink -f $YOUR_ARG)
将解析像 "./"
和 "../"
这样的相对路径/p>
也请考虑这一点 ( source ):
#!/bin/bash
dir_resolve()
{
cd "$1" 2>/dev/null || return $? # cd to desired directory; if fail, quell any error messages but return exit status
echo "`pwd -P`" # output full, link-resolved path
}
# sample usage
if abs_path="`dir_resolve \"$1\"`"
then
echo "$1 resolves to $abs_path"
echo pwd: `pwd` # function forks subshell, so working directory outside function is not affected
else
echo "Could not reach $1"
fi
关于linux - 在 bash 中扩展一个可能的相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7126580/