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