对于bash脚本可靠的方式来获得自身的完整路径? [英] Reliable way for a bash script to get the full path to itself?

查看:172
本文介绍了对于bash脚本可靠的方式来获得自身的完整路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要知道它的完整路径bash脚本。我试图找到这样做,没有相对或时髦的外观路径结束了一个广泛的兼容模式。我只需要支持的bash,不是SH,csh的,等等。

I have a bash script that needs to know its full path. I'm trying to find a broadly-compatible way of doing that without ending up with relative or funky-looking paths. I only need to support bash, not sh, csh, etc.

我已经发现迄今:


  1. 接受的答案为<一个href=\"http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in\">Can一个Bash脚本告诉哪个目录下的存储?的地址获得脚本的路径通过目录名$ 1,0 ,这是好的,但可能返回的相对的路径(如),如果要更改目录中的脚本,并有路径仍指向脚本目录这是一个问题。尽管如此,目录名将成为问题的一部分。

  1. The accepted answer to "Can a Bash script tell what directory it's stored in?" addresses getting the path of the script via dirname $0, which is fine, but that may return a relative path (like .), which is a problem if you want to change directories in the script and have the path still point to the script's directory. Still, dirname will be part of the puzzle.

接受的答案为 Bash脚本绝对路径与OSX (OS X的具体,但得到的答复工作,无论)的给出了将测试,看看是否 $ 1,0 看起来相对,如果是将美元的功能p $ p-挂起 $ PWD 它。但结果仍然可以有相对位它(虽然总体来说是绝对的)NBSP;&MDASH;例如,如果脚本 T 目录的/ usr / bin中你们在<$ C $是C>的/ usr 键,键入斌/../斌/吨来运行它(是的,这是令人费解的),你结束了的/ usr / bin中/../斌作为脚本的目录路径。其中工作,但...

The accepted answer to "Bash script absolute path with OSX" (OS X specific, but the answer works regardless) gives a function that will test to see if $0 looks relative and if so will pre-pend $PWD to it. But the result can still have relative bits in it (although overall it's absolute) — for instance, if the script is t in the directory /usr/bin and you're in /usr and you type bin/../bin/t to run it (yes, that's convoluted), you end up with /usr/bin/../bin as the script's directory path. Which works, but...

的readlink 解决方案<一href=\"http://fritzthomas.com/open-source/linux/384-how-to-get-the-absolute-path-within-the-running-bash-script/\">on此页面,它看起来像这样:

The readlink solution on this page, which looks like this:

# Absolute path to this script. /home/user/bin/foo.sh
SCRIPT=$(readlink -f $0)
# Absolute path this script is in. /home/user/bin
SCRIPTPATH=`dirname $SCRIPT`

的readlink 不是POSIX和明显的解决方案依赖于GNU的的readlink ,其中BSD的将不会为工作某些原因(我没有获得一个类似BSD的系统检查)。

But readlink isn't POSIX and apparently the solution relies on GNU's readlink where BSD's won't work for some reason (I don't have access to a BSD-like system to check).

所以,这样做的各种方式,但他们都有自己的注意事项。

So, various ways of doing it, but they all have their caveats.

这将是一个更好的办法?其中,更好是指:

What would be a better way? Where "better" means:


  • 给我的绝对路径。

  • 在一个令人费解的方式调用时(上面#2见注释)甚至拿出时髦位。 (例如,至少适度canonicalizes的路径。)

  • 只依靠的bash-ISMS或事情是几乎可以肯定是在* nix系统中最流行的口味(的GNU / Linux,BSD和BSD-like系统像OS X,等等)。

  • 避免调用外部程序如果可能的话(例如,prefers bash的内置插件)。

  • 更新,感谢您的抬起头,至极)不具备解决符号链接(其实,我倒是那种$ p $的PFER它留给他们独处,但是这不是必须的)。

  • Gives me the absolute path.
  • Takes out funky bits even when invoked in a convoluted way (see comment on #2 above). (E.g., at least moderately canonicalizes the path.)
  • Relies only on bash-isms or things that are almost certain to be on most popular flavors of *nix systems (GNU/Linux, BSD and BSD-like systems like OS X, etc.).
  • Avoids calling external programs if possible (e.g., prefers bash built-ins).
  • (Updated, thanks for the heads up, wich) Doesn't have to resolve symlinks (in fact, I'd kind of prefer it left them alone, but that's not a requirement).

推荐答案

回答我的问题(占 FAQ )...

Answering my own question (per the FAQ)...

下面是我想出这似乎大多是适合我的好的标准:

Here's what I've come up with that seems to mostly fit my "better" criteria:

pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd`
popd > /dev/null

...但它似乎迂回和笨重。我希望别人将有一个更好的答案。

...but it seems roundabout and clunky. I'm hoping someone else will have a better answer.

另外请注意,如执行未从文件在所有即将在访问的文件系统(这是完全可能的)一个脚本,深奥的情况下,是不是迎合了那里(或任何其他的答案我见过)。

Note also that esoteric situations, such as executing a script that isn't coming from a file in an accessible file system at all (which is perfectly possible), is not catered to there (or in any of the other answers I've seen).

修改:第二行更改为:

SCRIPTPATH=`pwd -P`

...如果你想解决符号链接。 (我不知道,但其他人可能。)我不知道如何广泛支持的 -P 标志,虽然。 <一个HREF =htt​​p://stackoverflow.com/users/26428/dennis-williamson>丹尼斯·威廉姆森告诉我们,低于 -P 是可靠的的,好了,很久以前。 : - )

...if you want to resolve symlinks. (I don't, but others may.) I don't know how widely-supported the -P flag is, though.Dennis Williamson tells us below that -P is reliable as of, well, a long time ago. :-)

这篇关于对于bash脚本可靠的方式来获得自身的完整路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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