text 从脚本本身获取Bash脚本的源目录

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 从脚本本身获取Bash脚本的源目录相关的知识,希望对你有一定的参考价值。

#!/usr/bin/env 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:

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.
# 
# Beware: if you cd to a different directory before running this snippet, the result may be incorrect!
#https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself
# 
# 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:

#!/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'

以上是关于text 从脚本本身获取Bash脚本的源目录的主要内容,如果未能解决你的问题,请参考以下文章

如何从脚本本身中获取 Bash 脚本的源目录?

将 *unbuffered* 标准输出从 bash 脚本本身复制到文件

从 bash 脚本本身将 stdout 的副本重定向到日志文件

bash入门脚本(未完善)

被另一个bash脚本调用后获取文件的当前目录[重复]

linux shell脚本获取脚本目录时,$(dirname “${BASH_SOURCE[0]}“)与$(dirname $0)有什么区别?(脚本路径,脚本包含关系,父子脚本)