bash 如何获取当前shell脚本的完整路径名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2465161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the full pathname of the current shell script?
提问by Dan
Is there a less brute-force way to do this?
有没有一种不那么暴力的方法来做到这一点?
#!/bin/ksh
THIS_SCRIPT=$(/usr/bin/readlink -f $(echo SCRIPT_PATH=$(cd `dirname THIS_SCRIPT=$(/usr/bin/readlink -nf "##代码##")
`; pwd)
| /bin/sed "s,^[^/],$PWD/&,"))
echo $THIS_SCRIPT
I'm stuck using kshbut would prefer a solution that works in bashtoo (which I think this does).
我一直在使用,ksh但更喜欢一个也适用的解决方案bash(我认为确实如此)。
回答by Ignacio Vazquez-Abrams
Entry #28in the bash FAQ:
bash 常见问题中的条目 #28:
How do I determine the location of my script? I want to read some config files from the same place.
There are two prime reasons why this issue comes up: either you want to externalize data or configuration of your script and need a way to find these external resources, or your script is intended to act upon a bundle of some sort (eg. a build script), and needs to find the resources to act upon.
It is important to realize that in the general case, this problem has no solution. Any approach you might have heard of, and any approach that will be detailed below, has flaws and will only work in specific cases. First and foremost, try to avoid the problem entirely by not depending on the location of your script!
...
Using BASH_SOURCE
The BASH_SOURCE internal bash variable is actually an array of pathnames. If you expand it as a simple string, e.g. "$BASH_SOURCE", you'll get the first element, which is the pathname of the currently executing function or script.
如何确定我的脚本的位置?我想从同一个地方读取一些配置文件。
出现此问题的主要原因有两个:要么您想将脚本的数据或配置外部化并需要一种方法来查找这些外部资源,要么您的脚本旨在处理某种类型的包(例如构建脚本),并且需要找到要采取行动的资源。
重要的是要认识到,在一般情况下,这个问题没有解决方案。您可能听说过的任何方法以及下面将详细介绍的任何方法都有缺陷,并且仅适用于特定情况。首先,尽量不要依赖脚本的位置来完全避免这个问题!
...
使用 BASH_SOURCE
BASH_SOURCE 内部 bash 变量实际上是一个路径名数组。如果您将其扩展为一个简单的字符串,例如“$BASH_SOURCE”,您将获得第一个元素,即当前正在执行的函数或脚本的路径名。
回答by Dana Lacoste
I've always done:
我一直这样做:
##代码##I've never used readlink before: is it Gnu only? (i.e. will it work on HP-UX, AIX, and Solaris out of the box? dirname and pwd will....)
我以前从未使用过 readlink:它只是 Gnu 吗?(即它可以在 HP-UX、AIX 和 Solaris 上开箱即用吗?dirname 和 pwd 将......)
(edited to add `` which I forgot in original post. d'oh!) (edit 2 to put on two lines which I've apparently always done when I look at previous scripts I'd written, but hadn't remembered properly. First call gets path, second call eliminates relative path) (edit 3 fixed typo that prevented single line answer from working, back to single line!)
(编辑以添加我忘记在原始帖子中的`` 哦!)(编辑 2 以放置两行,当我查看以前编写的脚本时,我显然总是这样做,但没有正确记住. 第一个调用获取路径,第二个调用消除相对路径)(编辑 3 个阻止单行应答工作的固定错字,回到单行!)
回答by Dan
Why didn't I think to try this before I asked the question?
为什么我在问这个问题之前没想过试试这个?
##代码##Works great.
效果很好。
回答by Claudio
In macOS I use (edit: only works if you run the script from where the script actually is!)
在 macOS 中我使用(编辑:仅当您从脚本实际所在的位置运行脚本时才有效!)
my_script=$(pwd)/$(basename $0)
my_script=$(pwd)/$(basename $0)

