0

I am trying to do a search with find and pass the results into grep. Grep has to find the files matched against string1 and string2 and string3.

I have the following command:

#/bin/bash
searchpath="/home/myfolder"
string1="abc"
string2="def"
string3="ghi"
find `echo "${searchpath}"` -type f -print0 | xargs -0 grep -l -E '"${string1}".*"${string2}".*"${string3}"'

But the result is blank, but when I do:

find /home/myfolder -type f -print0 | xargs -0 grep -l -E 'abc.*def.*ghi'

I get results. What am I doing wrong?

4

2 回答 2

2

从行中删除单引号:

find `echo "${searchpath}"` -type f -print0 | xargs -0 grep -l -E '"${string1}".*"${string2}".*"${string3}"'

即,说:

find "${searchpath}" -type f -print0 | xargs -0 grep -l -E "${string1}".*"${string2}".*"${string3}"

会工作。当你用单引号括起来时,shell 将其解释为:

"${string1}".*"${string2}".*"${string3}"

(不扩展变量)

而且,你不用说

`echo "${searchpath}"` 

"${searchpath}"

就足够了。

于 2013-09-06T10:45:59.963 回答
2

您不需要使用命令替换。你应该使用""引用变量,而不是'

find "${searchpath}" -type f -print0 | xargs -0 grep -l -E "${string1}.*${string2}.*${string3}"
于 2013-09-06T10:46:03.890 回答