1

noted: this is a use-specific question, but I hope others can benefit from this too

To get a basic sense of what I want to do:

QProcess runs a command by:

QProcess::start("sh -c \"cd /tmp/tempdir ; ./my_script --option file.txt ; echo $?\" ")

The script expects input from the user (a password), QProcess::readAll() confirms the script's input request. The input is given by QProcess::write().

Here I get lost! -> No output is received. from readAll() and readAllStandardOutput(), both are empty.

I need the output: particularly the echo $? for later processing, but don't get anything since readAll() and readAllStandardOutput() returns empty strings

To me there are several possible issues to cause this, maybe more than one:

  • The script does not receive this input
  • The script receives the input, but expects a "return key press"
  • The script receives the input, QProcess::write automatically does a "return key press", the script continues and finishes but QProcess does not read this output

The Code:

// please ignore the messiness of the code, a placed a number of debugs just to see the code response.

cmd = "cd /tmp/tempdir ; ./my_script --option file.txt ; echo $?" (without quotes)

input => required input for script

QString gen_serv::runCommand(QString cmd, QString input){
    Process *p = new QProcess(parent);
    p->setProcessChannelMode(QProcess::MergedChannels);
    //    p->start("sh ", QStringList() << " -c " << cmd);
    QString c = QString("sh -c \"" + cmd + "\" ");
    p->start(c);                                                <---- ACTUAL COMMAND : sh -c "cd /tmp/tempdir ; ./my_script --option file.txt ; echo $?"
    if (p->waitForStarted()) {
        if (!p->waitForReadyRead()) {
            qDebug(log_lib_gen_serv) << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
        }
        if (!p->waitForFinished(1000)) {
            qDebug() << p->readAll();
            qDebug() << p->readAllStandardOutput();
            //p->write(input.toLatin1());
            p->write(QString(input + QString("\n")).toLatin1());           <--- added this "\n" incase the process/console was waiting for a return press, thus the /n should satisfy the return requirement
            qDebug() << p->readAll();
            qDebug() << p->readAllStandardOutput();
            if (!p->waitForFinished()) {
                qDebug() << p->readAll();
                qDebug() << p->readAllStandardOutput();
                qDebug(log_lib_gen_serv) << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
            }
            qDebug() << p->readAll();
            qDebug() << p->readAllStandardOutput();
        }
        QString s = QString(p->readAll() + p->readAllStandardOutput());
        return s;
    }
    else{
        qDebug(log_lib_gen_serv) << "waitForStarted() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
    }
    p->waitForFinished();
    p->kill();
    return QString();
}

Please note:

The script is long and complicated, however it returns a message "done" if successfully executed and an exit code for each case.

Thus the echo $? should capture and return this for processing i.e. return s

I hope this makes sense, any suggestions how I can attempt to solve this issue?

Community
  • 1
  • 1
CybeX
  • 2,060
  • 3
  • 48
  • 115
  • Is it the same question as [this](http://stackoverflow.com/questions/41848939/reading-and-writing-to-qprocess-in-qt-console-application)? – Tarod Jan 25 '17 at 15:31
  • @Tarod keen eye, yes, this one is where I was in the early stages of "the struggle" :p I will be closing this question as it is pointless. And not to lay it on anymore, but I really appreciate the help. I know it has saved me atleast a day of struggling! Thank you! – CybeX Jan 25 '17 at 19:52
  • You are a kind person :) Thanks to you. Happy coding :) – Tarod Jan 25 '17 at 19:57
  • 1
    Possible duplicate of [reading and writing to QProcess in Qt Console Application](http://stackoverflow.com/questions/41848939/reading-and-writing-to-qprocess-in-qt-console-application) – CybeX Jan 25 '17 at 21:28
  • For reference, don’t use `$?` like this: you get better and easier information from [QProcess::exitCode()](http://doc.qt.io/qt-5/qprocess.html#exitCode) (and `exitStatus`). – Davis Herring Sep 05 '18 at 01:57

0 Answers0