代码中需要调用shell,原写法为:
QProcess *proc = new QProcess(); QString qCmd = "find ./ -name *.so -print0 | xargs -0 objdump -x | grep -oE \"T_[0-9, a-f, A-F]{4}\" "; if (proc->waitForFinished(1000)) QByteArray qOutput = proc->readAllStandardOutput(); QList<QByteArray> list = qOutput.split('\n'); QList<QByteArray>::iterator itor = list.begin(); for ( ; itor != list.end(); itor++) QByteArray strline = *itor;
但输出结果一直为空,同样的shell语句,在终端中直接执行,输出结果非空。
后在一博客中找到说法,问题出在概念没搞清,管道符是由shell进行解析处理的,上面的用法,相当于是两个shell命令,不能在一个QProcess中处理。
当QProcess需要处理带管道的shell时,需通过如下方法:
QProcess *proc = new QProcess(); options << "find ./ -name *.so -print0 | xargs -0 objdump -x | grep -oE \"T_[0-9, a-f, A-F]{4}\""; proc->start("/bin/bash", options);
|