我有一个Bash脚本,需要知道它的完整路径。我试图找到一种广泛兼容的方式来做到这一点,而不会以相对或时髦的路径结束。我只需要支持Bash,不支持sh, csh等。

到目前为止,我发现:

The accepted answer to Getting the source directory of a Bash script from within addresses getting the path of the script via dirname $0, which is fine, but that may return a relative path (like .), which is a problem if you want to change directories in the script and have the path still point to the script's directory. Still, dirname will be part of the puzzle. The accepted answer to Bash script absolute path with OS X (OS X specific, but the answer works regardless) gives a function that will test to see if $0 looks relative and if so will pre-pend $PWD to it. But the result can still have relative bits in it (although overall it's absolute) — for instance, if the script is t in the directory /usr/bin and you're in /usr and you type bin/../bin/t to run it (yes, that's convoluted), you end up with /usr/bin/../bin as the script's directory path. Which works, but... The readlink solution on this page, which looks like this: # Absolute path to this script. /home/user/bin/foo.sh SCRIPT=$(readlink -f $0) # Absolute path this script is in. /home/user/bin SCRIPTPATH=`dirname $SCRIPT` But readlink isn't POSIX and apparently the solution relies on GNU's readlink where BSD's won't work for some reason (I don't have access to a BSD-like system to check).

有很多种方法,但都有注意事项。

还有什么更好的办法呢?“更好”的意思是:

Gives me the absolute path. Takes out funky bits even when invoked in a convoluted way (see comment on #2 above). (E.g., at least moderately canonicalizes the path.) Relies only on Bash-isms or things that are almost certain to be on most popular flavors of *nix systems (GNU/Linux, BSD and BSD-like systems like OS X, etc.). Avoids calling external programs if possible (e.g., prefers Bash built-ins). (Updated, thanks for the heads up, wich) It doesn't have to resolve symlinks (in fact, I'd kind of prefer it left them alone, but that's not a requirement).


当前回答

也许下面这个问题的公认答案会有所帮助。

如何在Mac上获得GNU的readlink -f的行为?

假设您只想规范化从连接$PWD和$0得到的名称(假设$0一开始就不是绝对的),那么只需沿abs_dir=${abs_dir//\/一行使用一系列正则表达式替换即可。\//\/}等。

是的,我知道这看起来很糟糕,但它会起作用,而且是纯粹的Bash。

其他回答

被接受的解决方案(对我来说)不方便“来源”: 如果你从“来源../..”/yourScript", $0将是"bash"!

下面的函数(对于bash >= 3.0)给出了正确的路径,但是脚本可能会被调用(直接或通过源代码,使用绝对路径或相对路径): (这里的“正确路径”指的是被调用脚本的完整绝对路径,即使是从另一个路径直接调用,也可以使用“source”)

#!/bin/bash
echo $0 executed

function bashscriptpath() {
  local _sp=$1
  local ascript="$0"
  local asp="$(dirname $0)"
  #echo "b1 asp '$asp', b1 ascript '$ascript'"
  if [[ "$asp" == "." && "$ascript" != "bash" && "$ascript" != "./.bashrc" ]] ; then asp="${BASH_SOURCE[0]%/*}"
  elif [[ "$asp" == "." && "$ascript" == "./.bashrc" ]] ; then asp=$(pwd)
  else
    if [[ "$ascript" == "bash" ]] ; then
      ascript=${BASH_SOURCE[0]}
      asp="$(dirname $ascript)"
    fi  
    #echo "b2 asp '$asp', b2 ascript '$ascript'"
    if [[ "${ascript#/}" != "$ascript" ]]; then asp=$asp ;
    elif [[ "${ascript#../}" != "$ascript" ]]; then
      asp=$(pwd)
      while [[ "${ascript#../}" != "$ascript" ]]; do
        asp=${asp%/*}
        ascript=${ascript#../}
      done
    elif [[ "${ascript#*/}" != "$ascript" ]];  then
      if [[ "$asp" == "." ]] ; then asp=$(pwd) ; else asp="$(pwd)/${asp}"; fi
    fi  
  fi  
  eval $_sp="'$asp'"
}

bashscriptpath H
export H=${H}

关键是检测“source”大小写,并使用${BASH_SOURCE[0]}返回实际的脚本。

Use:

SCRIPT_PATH=$(dirname `which $0`)

它将可执行文件的完整路径打印到标准输出,该路径是在shell提示符下输入传入参数时执行的($0包含该参数)

Dirname从文件名中去掉非目录后缀。

因此,无论是否指定了路径,您最终都会得到脚本的完整路径。

我很惊讶这里没有提到realpath命令。我的理解是它可以广泛移植。

你的初始解决方案是:

SCRIPT=$(realpath "$0")
SCRIPTPATH=$(dirname "$SCRIPT")

并根据您的喜好留下未解决的符号链接:

SCRIPT=$(realpath -s "$0")
SCRIPTPATH=$(dirname "$SCRIPT")

如果我们使用Bash,我相信这是最方便的方式,因为它不需要调用任何外部命令:

THIS_PATH="${BASH_SOURCE[0]}";
THIS_DIR=$(dirname $THIS_PATH)

简单:

BASEDIR=$(readlink -f $0 | xargs dirname)

不需要花哨的运算符。