0

I'm trying to use QProcess to launch, read and write in a shell script.

I have an issue in a specific case, when the shell script use :

read -e -p "MyQuestion [yes/no]" RESPONSE

In this case, the shell script is waiting for the user to write yes or no, but the question is not displayed by my QProcess.

My script :

echo "toto"
read -e -p "Do you want to remove some of the patterns?[yes/no] " REPONSE
echo $REPONSE

In a classic terminal this is what is displayed :

toto
Do you want to remove some of the patterns?[yes/no] no  ==> waiting here for the user
no

With my QProcess :

toto ==> waiting here for the user
no

I have an object HProcess that herits QProcess, this is my start fonction :

void HProcess::start(const QString &program, const QStringList &arguments, OpenMode mode){
    QProcess::start(program, arguments, mode);
}

In this case :

program = "/bin/bash"
arguments[0] = "-c"
arguments[1] = "myscript.sh"
mode = QIODevice::ReadWrite

My HProcess is initialised this way :

void HProcess::init(){
    connect(this, SIGNAL(errorOccurred(QProcess::ProcessError)), this, SLOT(erreur(QProcess::ProcessError)));
    connect(this, SIGNAL(readyReadStandardOutput()), this, SLOT(lireMessage()));
    connect(this, SIGNAL(finished(int,QProcess::ExitStatus)),this, SLOT(fini(int,QProcess::ExitStatus)));
    setProcessChannelMode(QProcess::MergedChannels);
}

void HProcess::lireMessage(){
    QString message=readAllStandardOutput();
    afficher(message); ==> show the result in a graphic object
}


void HProcess::fini(int exitCode, QProcess::ExitStatus exitStatus){
    QString message;
    if(exitStatus==QProcess::CrashExit){
        message= "Attention un problème est survenue ! \n";
    }else{
        message= QString("Le script s'est déroulé normalement. Code de retour : %1. \n").arg(exitCode);
    }
    afficher(message);
    this->deleteLater();
}

void HProcess::erreur(QProcess::ProcessError code){
    QString message;
    switch (code) {
    case QProcess::FailedToStart:
        message="Le processus n'a pas pu démarrer.\n";
        break;
    case QProcess::Crashed:
        message="Le processus s'est arreté alors qu'il avait bien demarré.\n";
        break;
    case QProcess::Timedout:
        message="Le processus ne répond plus.\n";
        break;
    case QProcess::WriteError:
        message="Une erreur est apparu alors que nous tentions d'écrire dans le processus.\n";
        break;
    case QProcess::ReadError:
        message="Une erreur est apparu alors que nous tentions de lire le processus.\n";
        break;
    default:
        message="Erreur inconnue.\n";
        break;
    }
    message += QString("Code d'erreur %1.\n").arg(code);
    afficher(message);
}

So when the read -e -p is used in my script finished() readyReadStandardOutput() errorOccurred() are not called. The user and QProcess don't know they have to interact.

A solution could be to use :

echo "MyQuestion [yes/no]"
read RESPONSE

But I would like to know if there is a way to detect that the shell is in a ""waiting mode"". It could help me to make appear button or QTextEdit somewhere.

Thanks

  • 1
    Try the example https://stackoverflow.com/a/60046291/4149835 (without inheritance) . No, QProcess doesn't provide a way to detect "waiting mode" itself. Therefore you have to write your questions to a user in some printable way. – Vladimir Bershov Mar 20 '20 at 17:42
  • So basically, I need to echo my question, then do my read. And have a trick like, if there is a '?' I know I have to wait for an input. Not the best solution for me, because it's not my script but my client's. So I have to make him changes his script, and be sure he always has a '?' or something. Thanks anyway ! – bastien pinaquy Mar 20 '20 at 19:21

0 Answers0