简体   繁体   English

在QProcess中执行shell命令。

[英]Executing the shell command in QProcess.Piping the input

I am trying to pipe the commands and execute it, but I am not able to figure how to pipe it. 我试图通过管道传递命令并执行它,但是我无法弄清楚如何通过管道传递它。 I am trying to copy multiple files at once using the shell command 我正在尝试使用shell命令一次复制多个文件

for %I in (source) do copy %I (destination) 对于(来源)中的%I,请复制%I(目标)

QString files = "for %I in (source) do copy %I (destination)"
QProcess copy ;
copy.start(files);

I have to implement the piping to do that. 我必须实现管道来做到这一点。 for Eg. 例如

QProcess sh;
sh.start("sh", QStringList() << "-c" << "ifconfig | grep inet");

sh.waitForFinished();
QByteArray output = sh.readAll();
sh.close();

How can I implement piping for my copy process? 如何为复制过程实现管道?

Try this example: 试试这个例子:

QProcess sh;
sh.start( "sh", { "-c", "ifconfig | grep inet" } );

if ( !sh.waitForFinished( -1 ) )
{
    qDebug() << "Error:" << sh.readAllStandardError();
    return -1;
}

const auto output = sh.readAllStandardOutput();
// ...

waitForFinished() should be called in blocking mode and it must be checked if it was successful or not. 应该在阻塞模式下调用waitForFinished() ,并且必须检查它是否成功。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM