bash 在与当前脚本相同的目录中运行脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38978650/
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
Run a script in the same directory as the current script
提问by IQAndreas
I have two Bash scripts in the same folder (saved somewhere by the user who downloads the entire repository):
我在同一个文件夹中有两个 Bash 脚本(由下载整个存储库的用户保存在某处):
script.sh
is run by the userhelper.sh
is required and run byscript.sh
script.sh
由用户运行helper.sh
是必需的并由script.sh
The two scripts should be in the same directory. I need the first script to call the second one, but there are two problems:
这两个脚本应该在同一目录中。我需要第一个脚本来调用第二个,但是有两个问题:
- Knowing the current working directory is useless to me, because I don't know how the user is executing the first script (could be with
/usr/bin/script.sh
, with./script.sh
, or it could be with../Downloads/repo/scr/script.sh
) - The script
script.sh
will be changing to a different directory before callinghelper.sh
.
- 知道当前的工作目录对我来说毫无用处,因为我不知道用户是如何执行第一个脚本的(可能是 with
/usr/bin/script.sh
, with./script.sh
,也可能是 with../Downloads/repo/scr/script.sh
) - 脚本
script.sh
将在调用之前更改为不同的目录helper.sh
。
I can definitely hack together Bash that does this by storing the current directoryin a variable, but that code seems needlessly complicated for what I imagine is a very common and simple task.
我绝对可以通过将当前目录存储在变量中来将 Bash 组合在一起,但是对于我想象的一项非常常见且简单的任务来说,该代码似乎不必要地复杂化。
Is there a standard way to reliablycall helper.sh
from within script.sh
? And will work in any Bash-supported operating system?
是否有一种标准的方法可以从内部可靠地调用?并且可以在任何支持 Bash 的操作系统中工作吗?helper.sh
script.sh
回答by fedorqui 'SO stop harming'
Since $0
holds the full path of the script that is running, you can use dirname
against it to get the path of the script:
由于$0
保存了正在运行的脚本的完整路径,您可以使用dirname
它来获取脚本的路径:
#!/bin/bash
script_name=$ /tmp/a.sh
script_name: /tmp/a.sh
full path: /tmp
script_full_path=$(dirname "#!/bin/bash
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
bash ${__dir}/b.sh
")
echo "script_name: $script_name"
echo "full path: $script_full_path"
so if you for example store it in /tmp/a.sh
then you will see an output like:
因此,例如,如果您将其存储在其中,/tmp/a.sh
那么您将看到如下输出:
#!/bin/bash
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source ${__dir}/b.sh
so
所以
- Knowing the current working directory is useless to me, because I don't know how the user is executing the first script (could be with
/usr/bin/script.sh
, with./script.sh
, or it could be with../Downloads/repo/scr/script.sh
)
- 知道当前的工作目录对我来说毫无用处,因为我不知道用户是如何执行第一个脚本的(可能是 with
/usr/bin/script.sh
, with./script.sh
,也可能是 with../Downloads/repo/scr/script.sh
)
Using dirname "$0"
will allow you to keep track of the original path.
使用dirname "$0"
将允许您跟踪原始路径。
- The script
script.sh
will be changing to a different directory before callinghelper.sh
.
- 脚本
script.sh
将在调用之前更改为不同的目录helper.sh
。
Again, since you have the path in $0
you can cd
back to it.
同样,既然你有路径,$0
你就可以cd
回到它。
回答by Eugen
$0
is considered unsafeby many devs. I have found another solution, it is safe for a chain of bash scripts and source
.
$0
许多开发人员认为不安全。我找到了另一个解决方案,它对于一系列 bash 脚本和source
.
If a.sh
needs to execute b.sh
(located in the same folder) using a childbash process:
如果a.sh
需要b.sh
使用子bash 进程执行(位于同一文件夹中):
If a.sh
needs to execute b.sh
(located in the same folder) using the samebash process:
如果a.sh
需要b.sh
使用相同的bash 进程执行(位于同一文件夹中):