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?