Source bash script to another one

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

I have bash script test.sh which use functions from another search.sh script by following lines:

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

Both scripts are located in git repository. search.sh in <git_root>/scripts/ directory, test.sh is 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.sh approach ).

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

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:

  • Relative path of search.sh script towards <git_root> directory
  • What I want: To have ability to run test.sh from anywhere inside <git_root> without errors.

    PS: It is not possible to use permanent absolute path to search.sh as git repository can be cloned to any location.


    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 $0` > /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


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

     PATH="$GIT_REPO_LOCATION/scripts:$PATH"
     . search.sh
    

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

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

    GIT_LOCATION=$(find $HOME -name "search.sh" | head -1)
    GIT_SCRIPT_DIR=$(dirname $GIT_LOCATION)
    PATH="$GIT_SCRIPT_DIR:$PATH"
    . search.sh
    

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

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

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

    You could do this:

    # Get path the Git repo
    GIT_ROOT=`git rev-parse --show-toplevel`
    
    # Load the search functions
    source $GIT_ROOT/scripts/search.sh
    

    How get Git root directory!

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

    # Call the other script
    source $SCRIPTPATH/../scripts/search.sh
    # Or if it is in another path
    source $SCRIPTPATH/../scripts/seachers/search.sh

    The Apache Tomcat scripts use this approach:

    # resolve links - $0 may be a softlink
    PRG="$0"
    
    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"`
    

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

    链接地址: http://www.djcxy.com/p/56804.html

    上一篇: 为什么要改变LD

    下一篇: 源bash脚本到另一个