【发布时间】:2023-04-13 05:12:01
【问题描述】:
我正在使用 Qt 和 bash,需要执行以下操作:
bash: cat file | grep string
在 Qt 中:
QString cmd = "cat file | grep string";
QProcess *process = new QProcess;
process->start(cmd);
process->waitForBytesWritten();
process->waitForFinished();
qDebug() << process->readAll();
问题出在管道 ("|") 中,进程没有返回任何内容。如果没有(“|”),比如
"cat file"
一切正常。 我试过了。喜欢
"cat file \\| grep string",
"cat file \| grep string"
但结果是一样的。如果我复制命令并在 bash 中运行它一切正常。
QString::toAscii().data()
和其他转换也有不好的结果。
【问题讨论】:
-
试试
cmd = "bash -c 'cat file | grep string'"; -
@LaszloPapp 没有论据......因为有一个专用的API,你的答案会更好。另一方面,调用 shell 可能允许更复杂的命令,例如使用进程替换、shell globbing 等,因此在某些情况下它可能具有一些优势。
标签: c++ qt shell qprocess chain