一个Bash脚本可以告诉我们它的目录中存储? [英] Can a Bash script tell which directory it is stored in?

查看:182
本文介绍了一个Bash脚本可以告诉我们它的目录中存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何得到其中猛砸脚本位于从猛砸的目录路径脚本?

例如,假设我想使用Bash脚本为另一个应用程序启动器。我想改变工作目录到一个地方Bash脚本的位置,这样我就可以在该目录中的文件进行操作,就像这样:

 $ ./application 


解决方案

  DIR =$(光盘$(目录名称$ {BASH_SOURCE [0]})与&&放; PWD)

是一个有用的班轮,这将给你的脚本的完整目录名,无论它是从调用。

这将只要用于查找该脚本的路径的最后一个组件是不是一个符号链接(目录链接都OK)工作。如果你也想解析到脚本本身的任何链接,你需要多行的解决方案:

  SOURCE =$ {BASH_SOURCE [0]}
而[-h$ SOURCE];做#$决心SOURCE,直到文件已经不再是一个符号链接
  DIR =$(CD -P$(目录名称$源)与&&安培; PWD)
  SOURCE =$(的readlink$源)
  [[$ SOURCE = / *]!]&功放;&安培; SOURCE =$ DIR / $ SOURCE#如果$来源是一个相对的符号链接,我们需要解决它相对于放置符号文件所在的路径
DONE
DIR =$(CD -P$(目录名称$源)与&&安培; PWD)

这最后一个将与别名的任意组合,的bash -c ,符号链接等工作

请注意:如果您 CD 来运行这个片段之前不同的目录中,其结果可能是不正确的!此外,注意<一个href=\"http://bosker.word$p$pss.com/2012/02/12/bash-scripters-beware-of-the-cdpath/\"><$c$c>$CDPATH陷阱的。

要了解它是如何工作的,尝试运行这个更详细的形式:

 #!/斌/庆典SOURCE =$ {BASH_SOURCE [0]}
而[-h$ SOURCE];做#$决心SOURCE,直到文件已经不再是一个符号链接
  TARGET =$(的readlink$源)
  如果[[$ TARGET == / *];然后
    回声SOURCE$源是一个绝对符号链接'$ TARGET'
    SOURCE =$ TARGET
  其他
    DIR =$(目录名称$源)
    回声SOURCE$ SOURCE是一个相对的符号链接'$ TARGET(相对于'$ DIR')
    SOURCE =$ DIR / $ TARGET#如果$来源是一个相对的符号链接,我们需要解决它相对于放置符号文件所在的路径
  科幻
DONE
回声SOURCE是'$ SOURCE'
RDIR =$(目录名称$源)
DIR =$(CD -P$(目录名称$源)与&amp;&安培; PWD)
如果[$ DIR=$ RDIR!];然后
  回声DIR'$ RDIR解析为$ DIR'
科幻
回声DIR是'$ DIR'

和它将打印是这样的:

  SOURCE./scriptdir.sh是一个相对的符号链接SYM2 / scriptdir.sh'(相对于'。')
消息来源是./sym2/scriptdir.sh
DIR'./sym2解析为/家庭/ Ubuntu的/点文件/ FO FO /实际/ real1 / REAL2
DIR是一个'/ home / Ubuntu的/点文件/ FO FO /实际/ real1 / REAL2

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

For instance, let's say 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

解决方案

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && 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.

This 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 want to also 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" )" && 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" )" && 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! Also, watch out for $CDPATH gotchas.

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" )" && 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'

这篇关于一个Bash脚本可以告诉我们它的目录中存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆