展开在bash可能的相对路径 [英] Expand a possible relative path in bash

查看:178
本文介绍了展开在bash可能的相对路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为参数传递给我的剧本也有一些文件路径。那些可以,当然,是相对的(或包含〜)。但是对于功能我写我需要​​的是绝对路径,但没有自己的符号链接得到解决。

As arguments to my script there are some file paths. Those can, of course, be relative (or contain ~). But for the functions I've written I need paths that are absolute, but do not have their symlinks resolved.

有没有什么功能呢?

推荐答案

my_path的= $(-f的readlink $ YOUR_ARG)将解决像./和../

考虑这一点,以及( ):

Consider this as well (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

这篇关于展开在bash可能的相对路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆