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

标签 linux bash absolute-path

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

相关文章:

linux - Vulkan 内存堆及其内存类型

linux - 在 bash 中定义一个类似函数的宏

python - 安装 Python 时出错

linux - 在一台机器上模拟telnet通信

Linux Swap Usage Oneliner 仅输出数字

python - 在 Python 中运行 bash 命令 : os vs subprocess?

PHP:需要路径不适用于 cron 作业?

java - 从 JSP 更改 linux 的 PWD

python - 关于配置SQLite数据库的URI路径的困惑

linux - 如何在 bash 中使用换行符?