简体   繁体   English

bash cd 到指定目录

[英]bash cd up to specific directory

I'm working on an install script (written in bash) for a library.我正在为一个库编写一个安装脚本(用 bash 编写)。 The install script lives in a subdirectory of the project's base directory.安装脚本位于项目基目录的子目录中。 I'd like users to be able to run the install script from anywhere under the base directory, so I'm trying to make the script cd to the base directory before executing other commands.我希望用户能够从基本目录下的任何位置运行安装脚本,所以我试图在执行其他命令之前将脚本cd到基本目录。

I'm looking at something like我正在看类似的东西

while [[ $PWD == *"ProjectName"* ] && [ $PWD -ne *"ProjectName" ]]; do cd .. ;done

I'm open to cleaner solutions, but at the very least I'd like to understand why the above does not work.我对更清洁的解决方案持开放态度,但至少我想了解为什么上述方法不起作用。 No doubt I have brackets in the wrong spots, but I'm not sure where to put them.毫无疑问,我在错误的位置放置了括号,但我不确定将它们放在哪里。

The following does what I want.以下做我想要的。 Thanks to everyone (especially @David C. Rankin) for their helpful comments.感谢大家(尤其是@David C.Rankin)的有益评论。

while [[ "$PWD" = *"ProjectName"* && ! "$PWD" = *"ProjectName" ]]
      do
      cd ..
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM