来自QProcess的GNU查找-无法使用exec参数



我在一个运行良好的QProcess中运行GNU find。然而,当我尝试将它与grep结合时,它根本不会执行。

使用启动QProcess

process->start(findpattern.replace("~",QDir::home().absolutePath()), QProcess::Unbuffered | QProcess::ReadWrite);

当findpattern为例如时

find "~/Downloads" -name "*.cpp"

这很好用。但例如,查找包含"else"的所有cpp文件

find "~/Downloads" -name "*.cpp" -exec grep -l "else" {} ; 

失败

我做错了什么?

{} ;中反斜杠的目的是shell不会将分号解释为下一个命令的分隔符。这个反斜杠将被shell删除,而不会传递给find

但是QProcess直接运行find,而不是通过shell,所以反斜杠不应该存在。

来自find的手册页:

   -exec command ;
          Execute command; true if 0 status is returned.  All following arguments to
          find are taken to be arguments to the command until an argument consisting
          of `;' is encountered.  The string `{}' is replaced by  the  current  file
          name being processed everywhere it occurs in the arguments to the command,
          not just in arguments where it is alone, as  in  some  versions  of  find.
          Both  of  these  constructions  might  need  to be escaped (with a `') or
          quoted to protect them from expansion by the shell

最新更新