bash 引用与执行脚本相关的文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6659689/
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
Referring to a file relative to executing script
提问by Ryan Li
In a bash script I'm writing, I use source
to include the variable defined in a configuration file. The script to be executed is act.sh
, while the script to be source
d is act.conf.sh
, so in act.sh
I have:
在我正在编写的 bash 脚本中,我使用source
包含在配置文件中定义的变量。要执行act.sh
的脚本是,而要执行的脚本source
是act.conf.sh
,所以act.sh
我有:
source act.conf.sh
However this only works when running act.sh
in the directory containing it, since act.conf.sh
there refers to the file placed under the working directory. Is there a solution to make it refer to the file relative to the executing script without invoking cd
? Thanks.
然而,这仅act.sh
在包含它的目录中运行时才有效,因为act.conf.sh
这里指的是放置在工作目录下的文件。有没有办法让它在不调用的情况下引用与执行脚本相关的文件cd
?谢谢。
回答by Ignacio Vazquez-Abrams
请参阅:BASH 常见问题条目 #28:“如何确定脚本的位置?我想从同一位置读取一些配置文件。”
Any solution isn't going to work 100% of the time:
任何解决方案都不会在 100% 的情况下起作用:
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!
重要的是要认识到,在一般情况下,这个问题没有解决方案。您可能听说过的任何方法以及将在下面详述的任何方法都有缺陷,并且仅适用于特定情况。首先,尽量不要依赖脚本的位置来完全避免这个问题!
If you need to write a veryreusable tool, then taking the correct path as a parameter to your script is going to be the most reliable method.
如果您需要编写一个非常可重用的工具,那么将正确的路径作为脚本的参数将是最可靠的方法。
Assuming your script is only going to be run from certain shells, and only with a little bit of flexibility required, you can probably relax some of this paranoia. It is still good to look at your options. There are common patterns that people use that are particularly problematic.
假设您的脚本仅从某些 shell 运行,并且只需要一点灵活性,您可能可以放松一些这种偏执。看看你的选择仍然很好。人们使用的一些常见模式尤其成问题。
In particular, the FAQ recommends avoiding the very commonly used $0
variable:
特别是,FAQ 建议避免使用非常常用的$0
变量:
Nothing that reads
$0
will ever be bulletproof, because$0
itself is unreliable.
任何阅读
$0
都不会是防弹的,因为$0
它本身是不可靠的。
As an alternative, you could use $BASH_SOURCE
instead. Something like this:
作为替代方案,您可以$BASH_SOURCE
改用。像这样的东西:
source "${BASH_SOURCE%/*}/act.conf.sh"
There are some caveats to this solution, too. Check out the FAQ page to see the trade-offs between different solutions. They seem to recommend cd
in combination with $BASH_SOURCE
in cases where it will work for you, as you get a handy error condition when it fails to expand properly.
这个解决方案也有一些注意事项。查看常见问题页面以查看不同解决方案之间的权衡。他们似乎建议cd
在$BASH_SOURCE
它适合您的情况下结合使用,因为当它无法正确扩展时,您会遇到一个方便的错误情况。
回答by vagovszkym
See this: Bash: How _best_ to include other scripts?
I suggest to use:
我建议使用:
source $(dirname source ${BASH_SOURCE[0]/%act.sh/act.conf.sh}
)/act.conf.sh
回答by Clueless
Try the following:
请尝试以下操作:
##代码##