bash 检查 PWD 是否包含目录名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21097900/
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
Checking if PWD contains directory name
提问by zizther
I want to find out if the PWD contains a certain directory name in it, it should be able to test it being anywhere in the output.
我想知道 PWD 中是否包含某个目录名称,它应该能够测试它在输出中的任何位置。
For example I have structure paths like public/bower_components/name/
and also have paths which are just public
.
例如,我有像这样的结构路径public/bower_components/name/
,也有只是public
.
I want to test because the contents of the folder name
move into the public folder and the bower_components
folder is removed.
我想测试,因为文件夹的内容name
移动到公用文件夹中,bower_components
文件夹被删除。
Thanks
谢谢
回答by anubhava
You can use BASH regex for this:
您可以为此使用 BASH 正则表达式:
[[ "$PWD" =~ somedir ]] && echo "PWD has somedir"
OR using shell glob:
或使用 shell glob:
[[ "$PWD" == *somedir* ]] && echo "PWD has somedir"
回答by kojiro
You can use case
:
您可以使用case
:
case "$PWD" in
*/somedir/*) …;;
*) ;; # default case
esac
You can use [[
:
您可以使用[[
:
if [[ "$PWD" = */somedir/* ]]; then …
You can use regex:
您可以使用正则表达式:
if [[ "$PWD" =~ somedir ]]; then …
and there are more ways, to boot!
还有更多方法可以启动!