在 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.
Is there any function for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
MY_PATH=$(readlink -f $YOUR_ARG)
将解析相对路径,如"./"
和"../"
也请考虑这一点(来源):
MY_PATH=$(readlink -f $YOUR_ARG)
will resolve relative paths like"./"
and"../"
Consider this as well (source):
http:// /www.linuxquestions.org/questions/programming-9/bash-script-return-full-path-and-filename-680368/page3.html 具有以下
使用
pushd
/popd
进入pwd
有用的状态。http://www.linuxquestions.org/questions/programming-9/bash-script-return-full-path-and-filename-680368/page3.html has the following
which uses
pushd
/popd
to get into a state wherepwd
is useful.如果您的操作系统支持它,请使用:
并在变量中使用它:
这将扩展您的路径。在 Ubuntu 和 CentOS 上进行了测试,可能不适用于您的系统。有些人推荐 readlink,但 readlink 的文档说:
如果人们想知道我为什么引用变量,这是为了保留路径中的空格。就像执行
realpath some path
一样,会给你两个不同的路径结果。但realpath“some path”
将返回一个。引用参数 ftw :)感谢 NyanPasu64 的提醒。如果您不希望它遵循符号链接,则需要添加
-s
。If your OS supports it, use:
And using it in a variable:
Which will expand your path. Tested on Ubuntu and CentOS, might not be available on yours. Some recommend readlink, but documentation for readlink says:
In case people wonder why I quote my variables, it's to preserve spaces in paths. Like doing
realpath some path
will give you two different path results. Butrealpath "some path"
will return one. Quoted parameters ftw :)Thanks to NyanPasu64 for the heads up. You'll want to add
-s
if you don't want it to follow the symlinks.简单的一句:
用法:
我仍在尝试弄清楚如何让它完全不知道路径是否存在(因此也可以在创建文件时使用它)。
Simple one-liner:
Usage:
I am still trying to figure out how I can get it to be completely oblivious to whether the path exists or not (so it can be used when creating files as well).
这在 OS X 上对我有用:
$(cd SOME_DIRECTORY 2> /dev/null && pwd -P)
它应该可以在任何地方工作。其他解决方案似乎太复杂了。
This does the trick for me on OS X:
$(cd SOME_DIRECTORY 2> /dev/null && pwd -P)
It should work anywhere. The other solutions seemed too complicated.
使用 readlink -f,例如
Use
readlink -f <relative-path>
, e.g.也许这更具可读性,并且不使用子shell并且不更改当前目录:
Maybe this is more readable and does not use a subshell and does not change the current dir:
在 OS X 上,您可以
在 linux 上使用,您可能有
realpath
可执行文件。如果没有,以下方法可能有效(不仅适用于链接):on OS X you can use
on linux you might have
realpath
executable. if not, the following might work (not only for links):还有另一种方法。您可以在 bash 脚本中使用 python 嵌入来解析相对路径。
There's another method. You can use python embedding in bash script to resolve a relative path.
自我编辑,我刚刚注意到OP说他不寻找已解决的符号链接:
“但是对于我编写的函数,我需要绝对路径,但没有解决它们的符号链接。”
所以我猜这毕竟不太适合他的问题。 :)
因为这些年来我已经多次遇到这个问题,而这一次我需要一个可以在 OSX 和 Linux 上使用的纯 bash 便携式版本,所以我继续写了一个:
实时版本在这里:
https://github.com/keen99/shell-functions/tree/master/resolve_path
但为了这样,这是当前版本(我觉得它经过了很好的测试..但我愿意接受反馈!)
使其适用于普通的 bourne shell (sh) 可能并不困难,但我没有尝试......我太喜欢 $FUNCNAME 了。 :)
这是一个经典的例子,感谢brew:
使用这个函数,它将返回-real-路径:
self edit, I just noticed the OP said he's not looking for symlinks resolved:
"But for the functions I've written I need paths that are absolute, but do not have their symlinks resolved."
So guess this isn't so apropos to his question after all. :)
Since I've run into this many times over the years, and this time around I needed a pure bash portable version that I could use on OSX and linux, I went ahead and wrote one:
The living version lives here:
https://github.com/keen99/shell-functions/tree/master/resolve_path
but for the sake of SO, here's the current version (I feel it's well tested..but I'm open to feedback!)
Might not be difficult to make it work for plain bourne shell (sh), but I didn't try...I like $FUNCNAME too much. :)
here's a classic example, thanks to brew:
use this function and it will return the -real- path:
必须专门使用 bash 吗?我需要这样做,并且厌倦了 Linux 和 OS X 之间的差异。因此我使用 PHP 来实现快速但肮脏的解决方案。
我知道这不是一个非常优雅的解决方案,但它确实有效。
Do you have to use bash exclusively? I needed to do this and got fed up with differences between Linux and OS X. So I used PHP for a quick and dirty solution.
I know it's not a very elegant solution but it does work.