gpt4 book ai didi

linux - 在 bash 中扩展一个可能的相对路径

转载 作者:行者123 更新时间:2023-11-29 08:39:54 24 4
gpt4 key购买 nike

作为我脚本的参数,有一些文件路径。当然,这些可以是相对的(或包含 ~)。但是对于我编写的函数,我需要绝对路径,但不解析它们的符号链接(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com