Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
339 views
in Technique[技术] by (71.8m points)

bash - 从脚本本身获取Bash脚本的源目录(Get the source directory of a Bash script from within the script itself)

How do I get the path of the directory in which a Bash script is located, inside that script?

(如何获取其中的目录路径的Bash脚本所在,该脚本里面 ?)

For instance, let's say I want to use a Bash script as a launcher for another application.

(例如,假设我要使用Bash脚本作为另一个应用程序的启动器。)

I want to change the working directory to the one where the Bash script is located, so I can operate on the files in that directory, like so:

(我想将工作目录更改为Bash脚本所在的目录,以便可以对该目录中的文件进行操作,如下所示:)

$ ./application
  ask by community wiki translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"

is a useful one-liner which will give you the full directory name of the script no matter where it is being called from.

(是有用的单行代码,无论从何处调用脚本,它都会为您提供脚本的完整目录名称。)

It will work as long as the last component of the path used to find the script is not a symlink (directory links are OK).

(只要用于查找脚本的路径的最后一个组成部分不是符号链接(目录链接都可以),它将起作用。)

If you also want to resolve any links to the script itself, you need a multi-line solution:

(如果您还想解析到脚本本身的任何链接,则需要一个多行解决方案:)

#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
  SOURCE="$(readlink "$SOURCE")"
  [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"

This last one will work with any combination of aliases, source , bash -c , symlinks, etc.

(最后一个将使用别名, sourcebash -c ,symlinks等的任意组合。)

Beware: if you cd to a different directory before running this snippet, the result may be incorrect!

(请注意:如果在运行此代码段之前先将cd切换到其他目录,则结果可能不正确!)

Also, watch out for $CDPATH gotchas , and stderr output side effects if the user has smartly overridden cd to redirect output to stderr instead (including escape sequences, such as when calling update_terminal_cwd >&2 on Mac).

(另外,请注意$CDPATH gotchas和stderr输出的副作用,如果用户已巧妙地覆盖cd来将输出重定向到stderr(包括转义序列,例如在Mac上调用update_terminal_cwd >&2 )。)

Adding >/dev/null 2>&1 at the end of your cd command will take care of both possibilities.

(在cd命令的末尾添加>/dev/null 2>&1将解决这两种可能性。)

To understand how it works, try running this more verbose form:

(要了解其工作原理,请尝试运行以下更详细的形式:)

#!/bin/bash

SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
  TARGET="$(readlink "$SOURCE")"
  if [[ $TARGET == /* ]]; then
    echo "SOURCE '$SOURCE' is an absolute symlink to '$TARGET'"
    SOURCE="$TARGET"
  else
    DIR="$( dirname "$SOURCE" )"
    echo "SOURCE '$SOURCE' is a relative symlink to '$TARGET' (relative to '$DIR')"
    SOURCE="$DIR/$TARGET" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
  fi
done
echo "SOURCE is '$SOURCE'"
RDIR="$( dirname "$SOURCE" )"
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
if [ "$DIR" != "$RDIR" ]; then
  echo "DIR '$RDIR' resolves to '$DIR'"
fi
echo "DIR is '$DIR'"

And it will print something like:

(它会打印类似:)

SOURCE './scriptdir.sh' is a relative symlink to 'sym2/scriptdir.sh' (relative to '.')
SOURCE is './sym2/scriptdir.sh'
DIR './sym2' resolves to '/home/ubuntu/dotfiles/fo fo/real/real1/real2'
DIR is '/home/ubuntu/dotfiles/fo fo/real/real1/real2'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...