源 bash 脚本到另一个

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14237207/
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-18 04:12:28  来源:igfitidea点击:

Source bash script to another one

bashshell

提问by likern

Possible Duplicate:
Reliable way for a bash script to get the full path to itself?

可能的重复:
bash 脚本获取自身完整路径的可靠方法?

I have bash script test.shwhich use functions from another search.shscript by following lines:

我有 bash 脚本test.sh,它search.sh通过以下几行使用另一个脚本中的函数:

source ../scripts/search.sh
<call some functions from search.sh>

Both scripts are located in git repository. search.shin <git_root>/scripts/directory, test.shis located in the same directory (but, generally speaking, could be located anywhere inside <git_root>directory - I mean I can't rely on the following source search.shapproach ).

这两个脚本都位于 git 存储库中。search.sh<git_root>/scripts/目录中,test.sh位于同一目录中(但是,一般来说,可以位于<git_root>目录内的任何位置- 我的意思是我不能依赖以下source search.sh方法)。

When I call test.shscript from <git_root>/scripts/everything works well, but as soon as I change current working directory test.shfails:

当我test.sh<git_root>/scripts/一切中调用脚本时一切正常,但是一旦我更改当前工作目录test.sh就会失败:

cd <git_root>/scripts/
./test.sh         //OK
cd ..
./scripts/test.sh //FAILS
./scripts/test.sh: line 1: ../scripts/search.sh: No file or directory ...

Thus what I have:

因此我所拥有的:

  1. Relative path of search.shscript towards <git_root>directory
  1. search.sh脚本到<git_root>目录的相对路径

What I want:To have ability to run test.shfrom anywhere inside <git_root>without errors.

我想要的是:能够test.sh从内部任何地方运行<git_root>而不会出错。

P.S.:It is not possible to use permanent absolute path to search.shas git repository can be cloned to any location.

PS:无法使用永久绝对路径,search.sh因为 git 存储库可以克隆到任何位置。

回答by Some programmer dude

If both the scripts are in the same directory, then if you get the directory that the running script is in, you use that as the directory to call the other script:

如果两个脚本都在同一个目录中,那么如果您获得运行脚本所在的目录,您可以使用该目录作为调用另一个脚本的目录:

# Get the directory this script is in
pushd `dirname 
# Get path the Git repo
GIT_ROOT=`git rev-parse --show-toplevel`

# Load the search functions
source $GIT_ROOT/scripts/search.sh
` > /dev/null SCRIPTPATH=`pwd -P` popd > /dev/null # Now use that directory to call the other script source $SCRIPTPATH/search.sh

Taken from the accepted answer of the question I marked this question a duplicatre of: https://stackoverflow.com/a/4774063/440558

取自问题的已接受答案,我将此问题标记为重复:https: //stackoverflow.com/a/4774063/440558

回答by Rodrigo Catto

You could do this:

你可以这样做:

# Call the other script
source $SCRIPTPATH/../scripts/search.sh

How get Git root directory!

如何获取 Git 根目录

Or like @Joachim Pileborg says, but you have to pay attention that you must know the path of this one to another script;

或者像@Joachim Pileborg 说的那样,但你必须注意你必须知道这个脚本到另一个脚本的路径;

# Or if it is in another path
source $SCRIPTPATH/../scripts/seachers/search.sh
# resolve links - 
 PATH="$GIT_REPO_LOCATION/scripts:$PATH"
 . search.sh
may be a softlink PRG="
GIT_LOCATION=$(find $HOME -name "search.sh" | head -1)
GIT_SCRIPT_DIR=$(dirname $GIT_LOCATION)
PATH="$GIT_SCRIPT_DIR:$PATH"
. search.sh
" while [ -h "$PRG" ] ; do ls=`ls -ld "$PRG"` link=`expr "$ls" : '.*-> \(.*\)$'` if expr "$link" : '/.*' > /dev/null; then PRG="$link" else PRG=`dirname "$PRG"`/"$link" fi done PRGDIR=`dirname "$PRG"`

The Apache Tomcat scripts use this approach:

Apache Tomcat 脚本使用这种方法:

GIT_LOCATION=$(find $HOME -name ".git" -type d | head -1)
PATH="$GIT_LOCATION:$PATH"
. search.sh

Any way, you have to put this snippet on all scripts that use other scripts.

无论如何,您必须将此代码段放在使用其他脚本的所有脚本上。

回答by David W.

Is there a way to identify this Git repository location? An environment variable set? You could set PATHin the script itself to include the Git repository:

有没有办法识别这个 Git 存储库位置?环境变量集?您可以PATH在脚本本身中设置以包含 Git 存储库:

git_root=""
while /bin/true ; do
    if [[ "$(pwd)" == "$HOME" ]] || [[ "$(pwd)" == "/" ]] ; then
        break
    fi

    if [[ -d ".git" ]] ; then
        git_root="$(pwd)"
        break
    fi

    cd ..
done

Once the script is complete, your PATHwill revert to its old value, and $GIT_REPO_LOCATION/scriptswill no longer be part of the PATH.

脚本完成后,您PATH将恢复到其旧值,并且$GIT_REPO_LOCATION/scripts不再是PATH.

The question is finding this location to begin with. I guess you could do something like this in your script:

问题是找到这个位置开始。我想你可以在你的脚本中做这样的事情:

if [[ -n "$git_root" ]] ; then
    . ${git_root}/scripts/search.sh
fi

By the way, now that $PATHis set, I can call the script via search.shand not ./search.shwhich you had to do when you were in the scripts directory, and your PATHdidn't include .which is the current directory (and PATH shouldn't include .because it is a security hole).

顺便说一句,既然$PATH已经设置,我可以通过调用脚本,search.sh而不是./search.sh在脚本目录中时您必须执行的操作,并且您PATH没有包含.当前目录(并且 PATH 不应该包含,.因为它是一个安全漏洞)。

One more note, you could search for the .gitdirectory too which might be the Git repository you're looking for:

还要注意的是,您也可以搜索.git目录,这可能是您要查找的 Git 存储库:

##代码##

回答by Ian Stapleton Cordasco

For the people who would rather not use git's features for finding the parent directory. If you can be sure you'll always be running the script from within the git directory, you can use something like this:

对于那些不想使用 git 的功能来查找父目录的人。如果您可以确定您将始终从 git 目录中运行脚本,您可以使用以下内容:

##代码##

I haven't tested this but it will just loop back until it hits your home directory or / and it will see if there is a .gitdirectory in each parent directory. If there is, it sets the git_rootvariable and it will break out. If it doesn't find one, git_rootwill just be an empty string. Then you can do:

我还没有测试过这个,但它只会循环返回,直到它到达您的主目录或 / 并且它会查看.git每个父目录中是否有一个目录。如果有,它设置git_root变量,它会爆发。如果没有找到,git_root将只是一个空字符串。然后你可以这样做:

##代码##

IHTH

IHTH