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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:23:58  来源:igfitidea点击:

Run a script in the same directory as the current script

bashpathfilepath

提问by IQAndreas

I have two Bash scripts in the same folder (saved somewhere by the user who downloads the entire repository):

我在同一个文件夹中有两个 Bash 脚本(由下载整个存储库的用户保存在某处):

  • script.shis run by the user
  • helper.shis required and run by script.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:

这两个脚本应该在同一目录中。我需要第一个脚本来调用第二个,但是有两个问题:

  1. 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)
  2. The script script.shwill be changing to a different directory before calling helper.sh.
  1. 知道当前的工作目录对我来说毫无用处,因为我不知道用户是如何执行第一个脚本的(可能是 with /usr/bin/script.sh, with ./script.sh,也可能是 with ../Downloads/repo/scr/script.sh
  2. 脚本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.shfrom within script.sh? And will work in any Bash-supported operating system?

是否有一种标准的方法可以从内部可靠地调用?并且可以在任何支持 Bash 的操作系统中工作吗?helper.shscript.sh

回答by fedorqui 'SO stop harming'

Since $0holds the full path of the script that is running, you can use dirnameagainst 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.shthen you will see an output like:

因此,例如,如果您将其存储在其中,/tmp/a.sh那么您将看到如下输出:

#!/bin/bash
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source ${__dir}/b.sh

so

所以

  1. 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)
  1. 知道当前的工作目录对我来说毫无用处,因为我不知道用户是如何执行第一个脚本的(可能是 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"将允许您跟踪原始路径。

  1. The script script.shwill be changing to a different directory before calling helper.sh.
  1. 脚本script.sh将在调用之前更改为不同的目录helper.sh

Again, since you have the path in $0you can cdback to it.

同样,既然你有路径,$0你就可以cd回到它。

回答by Eugen

$0is 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.shneeds to execute b.sh(located in the same folder) using a childbash process:

如果a.sh需要b.sh使用bash 进程执行(位于同一文件夹中):

##代码##

If a.shneeds to execute b.sh(located in the same folder) using the samebash process:

如果a.sh需要b.sh使用相同的bash 进程执行(位于同一文件夹中):

##代码##