Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.93 KB | None | 0 0
  1. // please ignore the messiness of the code, a placed a number of debugs just to see the code response.
  2.  
  3. cmd = "cd /tmp/tempdir ; ./my_script file.txt ; echo $?"      (without quotes)
  4. input => required input from script
  5.  
  6. QString gen_serv::runCommand(QString cmd, QString input){
  7.     Process *p = new QProcess(parent);
  8.     p->setProcessChannelMode(QProcess::MergedChannels);
  9.     //    p->start("sh ", QStringList() << " -c " << cmd);
  10.     QString c = QString("sh -c \"" + cmd + "\" ");
  11.     p->start(c);
  12.     if (p->waitForStarted()) {
  13.         if (!p->waitForReadyRead()) {
  14.             qDebug(log_lib_gen_serv) << "waitForReadyRead() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
  15.         }
  16.         if (!p->waitForFinished(1000)) {
  17.             qDebug() << p->readAll();
  18.             qDebug() << p->readAllStandardOutput();
  19.             //p->write(input.toLatin1());
  20.             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
  21.             qDebug() << p->readAll();
  22.             qDebug() << p->readAllStandardOutput();
  23.             if (!p->waitForFinished()) {
  24.                 qDebug() << p->readAll();
  25.                 qDebug() << p->readAllStandardOutput();
  26.                 qDebug(log_lib_gen_serv) << "waitForFinished() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
  27.             }
  28.             qDebug() << p->readAll();
  29.             qDebug() << p->readAllStandardOutput();
  30.         }
  31.         QString s = QString(p->readAll() + p->readAllStandardOutput());
  32.         return s;
  33.     }
  34.     else{
  35.         qDebug(log_lib_gen_serv) << "waitForStarted() [false] : CODE: " << QVariant(p->error()).toString() << " | ERROR STRING: " << p->errorString();
  36.     }
  37.     p->waitForFinished();
  38.     p->kill();
  39.     return QString();
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement