bash 如何在zsh中获取执行脚本的父文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11685135/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get parent folder of executing script in zsh?
提问by don_jones
In bash i get the executing script's parent folder name by this line
在 bash 中,我通过这一行获取执行脚本的父文件夹名称
SCRIPT_PARENT=`readlink -f ${BASH_SOURCE%/*}/..`
Is there any way to achieve this in zsh in a way that works both in zsh and bash?
有什么方法可以在 zsh 和 bash 中以同时适用的方式在 zsh 中实现这一点?
Assume i have got a file /some/folder/rootfolder/subfolder/scriptwith the contents:
假设我有一个/some/folder/rootfolder/subfolder/script包含以下内容的文件:
echo `magic-i-am-looking-for`
I want it to behave this way:
我希望它的行为方式如下:
$ cd /some/other/folder
$ . /some/folder/rootfolder/subfolder/script
/some/folder/rootfolder
$ . ../../folder/rootfolder/subfolder/script
/some/folder/rootfolder
$ cd /some/folder/rootfolder
$ . subfolder/script
/some/folder/rootfolder
$ cd subfolder
$ . script
/some/folder/rootfolder
This should work in bash and zsh. My first implements this behavior, but does due to $BASH_SOURCEnot work in zsh.
这应该适用于 bash 和 zsh。我的第一个实现了这种行为,但由于$BASH_SOURCE在 zsh 中不起作用。
So basically its:
所以基本上它的:
Is there a way to emulate $BASH_SOURCEin zsh, that also works in bash?
有没有办法$BASH_SOURCE在 zsh 中进行模拟,这也适用于 bash?
回答by don_jones
I now realized that $0in zsh behaves like $BASH_SOURCEin bash. So using $BASH_SOURCEwhen available and falling back to $0solves my problem:
我现在意识到$0在 zsh 中的行为就像$BASH_SOURCE在 bash 中一样。因此,$BASH_SOURCE在可用时使用并回退来$0解决我的问题:
${BASH_SOURCE:-zsh> cat ../script
echo $0: SCRIPT_SOURCE=${0%/*}
echo $BASH_SOURCE: $BASH_SOURCE
echo '${BASH_SOURCE:-SCRIPT_SOURCE=$(/bin/readlink -f ${0%/*})
}:' ${BASH_SOURCE:-echo ${LOCATION%/*}
}
zsh> . script
LOCATION=SCRIPT_PARENT=$(readlink -f "$(which ##代码##)/..")
<source script here>
: script
$BASH_SOURCE:
${BASH_SOURCE:-##代码##}: script
bash> . script
##代码##: bash
$BASH_SOURCE: /home/me/script
${BASH_SOURCE:-##代码##}: /home/me/script
}
There is a little zsh edge case left, when sourcing from $PATHlike:
从$PATH以下来源采购时,还剩下一些 zsh 边缘案例:
I could do a which scriptbut this would not play nice with other cases
我可以做一个,which script但这在其他情况下不会很好
回答by Matthew Franglen
While it would be easy to do this in zsh, it is just as easy to use pure bash which is able to be evaluated in zsh. If you cannot use any command that may or may not be on your path, then you can only use variable alteration to achieve what you want:
虽然在 zsh 中很容易做到这一点,但使用能够在 zsh 中进行评估的纯 bash 也同样容易。如果您不能使用任何可能在您的路径上或可能不在您的路径上的命令,那么您只能使用变量更改来实现您想要的:
##代码##This is likely to be a relative path. If you really want the full path then you will have to resort to an external command (you couldimplement it yourself, but it would be a lot of work to avoid using a very available command):
这很可能是一个相对路径。如果您真的想要完整路径,那么您将不得不求助于外部命令(您可以自己实现它,但是避免使用非常可用的命令需要做很多工作):
##代码##This doesn't depend on your $PATH, it just depends on /bin/readlink being present. Which it almost certainly is.
这不取决于您的 $PATH,它只取决于 /bin/readlink 的存在。几乎可以肯定。
Now, you wanted this to be a sourced file. This is fine, as you can just export any variable you set, however if you execute the above then $0 will be the location of the sourced file and not the location of the calling script.
现在,您希望这是一个源文件。这很好,因为您可以导出您设置的任何变量,但是如果您执行上述操作,则 $0 将是源文件的位置,而不是调用脚本的位置。
This just means you need to set a variable to hold the $0 value which the sourced script knows about. For example:
这只是意味着您需要设置一个变量来保存源脚本知道的 $0 值。例如:
The script you will source:
您将获取的脚本:
##代码##The script that sources that script:
获取该脚本的脚本:
##代码##But given that the ${0%/*} expansion is so compact, you could just use that in place of the script.
但鉴于 ${0%/*} 扩展如此紧凑,您可以使用它来代替脚本。
回答by shkschneider
Because you were able to run the command from your $PATH I'll do something like that:
因为您可以从 $PATH 运行命令,所以我将执行以下操作:
##代码##Is that your desired output?
这是您想要的输出吗?

