如何从脚本本身中获取 Bash 脚本的源目录? [英] How can I get the source directory of a Bash script from within the script itself?
问题描述
如何获取Bash 脚本所在目录的路径位于里面那个脚本?
How do I get the path of the directory in which a Bash script is located, inside that script?
我想使用 Bash 脚本作为另一个应用程序的启动器.我想将工作目录更改为 Bash 脚本所在的目录,这样我就可以对该目录中的文件进行操作,如下所示:
I want to use a Bash script as a launcher for another application. 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:
$ ./application
推荐答案
#!/usr/bin/env bash
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && 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:
#!/usr/bin/env 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 )"
最后一个将适用于别名、source
、bash -c
、符号链接等的任意组合.
This last one will work with any combination of aliases, source
, bash -c
, symlinks, etc.
注意:如果在运行此代码段之前cd
到不同的目录,结果可能不正确!
Beware: if you cd
to a different directory before running this snippet, the result may be incorrect!
另外,注意$CDPATH
问题 和 stderr 输出副作用,如果用户巧妙地覆盖了 cd 以将输出重定向到 stderr(包括转义序列,例如调用 update_terminal_cwd >&2
在 Mac 上).在 >/dev/null 2>&1
命令的末尾添加 >/dev/null 2&1
将处理这两种可能性.
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). Adding >/dev/null 2>&1
at the end of your cd
command will take care of both possibilities.
要了解它是如何工作的,请尝试运行这个更详细的表单:
To understand how it works, try running this more verbose form:
#!/usr/bin/env 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'"
它会打印如下内容:
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'
这篇关于如何从脚本本身中获取 Bash 脚本的源目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!