这是一个有用的单行程序,无论从哪里调用脚本,它都会为您提供脚本的完整目录名称。' H- d/ e6 \! d7 K4 j0 g# a- u
只要用于查找脚本路径的最后一个组件不是符号链接(目录链接正常),它就会工作。如果您想分析脚本本身的任何链接,您需要更多的解决方案:5 |" j" l3 [0 D, L( S3 l5 F1 y1 F7 r
#!/usr/bin/env bashSOURCE=${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 locateddoneDIR=$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )2 f' I* W" l1 M# w
最后一个将适用于别名,source、bash -c、 任何组合,如符号链接。9 u4 S. M$ g# a: b+ q 注意:如果您cd在运行此代码段之前,结果可能不正确!5 z6 x5 r2 @, c/ J, t& X s7 Q
另外,如果用户巧妙地覆盖 cd将输出重定向 stderr(包括转义序列,如 Mac 上调时请注意)$CDPATHgotchas和 stderr 输出副作用update_terminal_cwd >&2。>/dev/null 2>&1在cd在命令结束时添加这两种可能性。4 D- z8 Z' N8 d6 O* V5 e4 @* A
要了解它是如何工作的,请尝试操作更详细的表格: % T5 M0 v& B( W
#!/usr/bin/env bashSOURCE=${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 fidoneecho "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'"fiecho "DIR is '$DIR'" + O. \7 U, p: ?) K) [) {7 U/ O
它将打印以下内容:4 I; B& J4 H: q- u1 S0 Q: Y
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' + M; r# g5 V# ~2 e' E5 m% D