> find /etc -name 'shells'
/etc/shells # good !!
> SEARCH="-name 'shells'"; find /etc $SEARCH
# nothing found - bad !!
Run Code Online (Sandbox Code Playgroud)
为什么“find”命令不能接受变量中的参数?
其他命令在这种模式下工作正常。它可能与空格和解析有关。我如何首先在变量中构造参数,然后使用此参数执行“查找”?
要清楚,我想制作 -name xxxx -o -name yyyyy -o -name zzzzz 的链,然后通过一次运行找到所有文件
你的问题是简单的引号没有被解释为这样,而是在你的参数中。
你认为你已经执行了这个:
find /etc -name 'shells'
Run Code Online (Sandbox Code Playgroud)
实际上,当您执行此操作时:
find /etc -name \'shells\'
Run Code Online (Sandbox Code Playgroud)
请记住:在 bash 中,双引号内的简单引号不会被忽略。
所以解决方案是不要加上任何简单的引号:
SEARCH="-name shells"; find /etc $SEARCH
Run Code Online (Sandbox Code Playgroud)
更好的解决方案是使用引号,然后使用 eval:
SEARCH="-name 'shells'"; eval " find /etc $SEARCH"
Run Code Online (Sandbox Code Playgroud)
安全问题:永远不要在 eval 参数中使用用户提供的信息。