在bash中展开可能的相对路径

作为我的脚本的参数有一些文件路径。那些当然可以是相对的(或包含〜)。但对于我写的函数,我需要的路径是绝对的,但没有它们的符号链接解决

这有什么功能吗?

MY_PATH = $(readlink -f $ YOUR_ARG)将解析相对路径,例如“./”和“../”

考虑这一点(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

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...