BASH find -name(读取变量)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15513661/
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
BASH find -name (read variable)
提问by Adam Spiers
I'm new to bash and have encountered a problem i can't solve. The issue is i need to use find -name with a name defined as a variable. Part of the script:
我是 bash 新手,遇到了我无法解决的问题。问题是我需要将 find -name 与定义为变量的名称一起使用。部分脚本:
read MYNAME
find -name $MYNAME
But when i run the script, type in '*sh' for read, there are 0 results. However, if i type directly in the terminal:
但是当我运行脚本时,输入 '*sh' 进行读取,结果为 0。但是,如果我直接在终端中输入:
find -name '*sh'
it's working fine.
它工作正常。
I also tried
我也试过
read MYNAME
find -name \'$MYNAME\'
with typing *sh for read and no success.
输入 *sh 进行读取但没有成功。
Can anyone help me out?
谁能帮我吗?
回答by mikyra
Most probably
最可能
read MYNAME
find -name "$MYNAME"
is the version you are looking for. Without the double quotes "the shell will expand *in your *shexample prior to running findthat's why your first attempt didn't work
是您要找的版本。如果没有双引号",shell 将*在*sh运行之前在您的示例中展开,find这就是您的第一次尝试不起作用的原因
回答by Adam Spiers
You probably want
你可能想要
find -name "$MYNAME"
since this prevents $MYNAMEfrom being subject to bash's pathname expansion (a.k.a. "globbing"), resulting in *shbeing passed intact to find. One key difference is that globbing will not match hidden files such as .ssh, whereas find -name "*sh"will. However since you don't define the expected behaviour you are seeking, it's hard to say what you need for sure.
因为这可以防止$MYNAME受到 bash 的路径名扩展(又名“globbing”)的影响,从而导致*sh被完整地传递到find. 一个主要区别是通配符不会匹配隐藏文件,例如.ssh, 而find -name "*sh"会。但是,由于您没有定义您正在寻求的预期行为,因此很难确定您需要什么。

