我正在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" {} \;
失败
我做错了什么?
答案 0 :(得分:0)
{} \;
中反斜杠的目的是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