如何获取当前shell脚本的完整路径名?

发布于 2024-08-25 03:55:58 字数 209 浏览 7 评论 0原文

有没有更简单的方法来做到这一点?

#!/bin/ksh
THIS_SCRIPT=$(/usr/bin/readlink -f $(echo $0 | /bin/sed "s,^[^/],$PWD/&,"))
echo $THIS_SCRIPT

我一直在使用 ksh,但更喜欢在 bash 中也能工作的解决方案(我认为确实如此)。

Is there a less brute-force way to do this?

#!/bin/ksh
THIS_SCRIPT=$(/usr/bin/readlink -f $(echo $0 | /bin/sed "s,^[^/],$PWD/&,"))
echo $THIS_SCRIPT

I'm stuck using ksh but would prefer a solution that works in bash too (which I think this does).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

↘紸啶 2024-09-01 03:55:58

bash 常见问题解答中的 条目 #28

如何确定脚本的位置?我想从同一个地方读取一些配置文件。

出现此问题有两个主要原因:要么您想要外部化脚本的数据或配置,并且需要一种方法来查找这些外部资源,要么您的脚本旨在对某种捆绑包进行操作(例如. 构建脚本),并且需要找到要执行操作的资源。

重要的是要认识到,在一般情况下,这个问题没有解决方案。您可能听说过的任何方法以及下面将详细介绍的任何方法都存在缺陷,并且仅在特定情况下有效。首先也是最重要的,尝试通过不依赖脚本的位置来完全避免该问题!

...

使用 BASH_SOURCE

BASH_SOURCE 内部 bash 变量实际上是一个路径名数组。如果将其展开为一个简单的字符串,例如“$BASH_SOURCE”,您将获得第一个元素,它是当前正在执行的函数或脚本的路径名。

Entry #28 in the bash FAQ:

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.

落日海湾 2024-09-01 03:55:58

我一直这样做:

SCRIPT_PATH=$(cd `dirname ${0}`; pwd)

我以前从未使用过 readlink:它只是 Gnu 吗? (即它可以在 HP-UX、AIX 和 Solaris 上开箱即用吗?dirname 和 pwd 会......)

(编辑添加``,我在原始帖子中忘记了。d'oh!)
(编辑 2 添加两行,当我查看以前编写的脚本时,我显然总是这样做,但没有正确记住。第一次调用获取路径,第二次调用消除相对路径)
(编辑 3 个固定的拼写错误,导致单行答案无法工作,回到单行!)

I've always done:

SCRIPT_PATH=$(cd `dirname ${0}`; pwd)

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....)

(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!)

新一帅帅 2024-09-01 03:55:58

为什么我在问问题之前没有想到尝试一下?

THIS_SCRIPT=$(/usr/bin/readlink -nf "$0")

效果很好。

Why didn't I think to try this before I asked the question?

THIS_SCRIPT=$(/usr/bin/readlink -nf "$0")

Works great.

不顾 2024-09-01 03:55:58

在 macOS 中,我使用(编辑:仅当您从脚本实际所在的位置运行脚本时才有效!)

my_script=$(pwd)/$(basename $0)

In macOS I use (edit: only works if you run the script from where the script actually is!)

my_script=$(pwd)/$(basename $0)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文