Few Shell Commands Doesn't Work When I Invoke a Script via Qprocess in Qt

Error while running a .sh script via QProcess

  1. It looks strange that you are using external script to update database!
  2. why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
  3. I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{
QProcess::startDetached("/bin/sh",
QStringList()<<"/home/aj/script.sh",
"<location of: 'ora_exported.csv' file>");
}

QProcess fails to execute a terminal line command via QPushButton

Try this:

header:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QProcess>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();

private slots:
void on_executeScriptBtn_clicked();

private:
Ui::MainWindow *ui;
QProcess * executeBash;
};
#endif // MAINWINDOW_H

source:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->executeBash = new QProcess(this);
this->executeBash->setProcessChannelMode(QProcess::MergedChannels);
connect(this->executeBash, &QProcess::readyReadStandardOutput, [script = this->executeBash](){
qDebug() << "[EXEC] DATA: " << script->readAll();
});
connect(this->executeBash, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
[script = this->executeBash](int exitCode, QProcess::ExitStatus exitStatus){
qDebug() << "[EXEC] FINISHED: " << exitCode << exitStatus;
if(script->bytesAvailable() > 0)qDebug() << "[EXEC] buffered DATA:" << script->readAll();
});
connect(this->executeBash, &QProcess::errorOccurred, [script = this->executeBash](QProcess::ProcessError error){
qDebug() << "[EXEC] error on execution: " << error << script->errorString();
});

}
void MainWindow::on_executeScriptBtn_clicked()
{
qDebug() << "Button clicked!"; // if you don't see this message check your SIGNAL/SLOT connections!
//this->executeBash->execute(...) // <- will wait till script is finished and block main thread
this->executeBash->start(QStringLiteral("/bin/sh"), QStringList() << QStringLiteral("/home/emanuele/Desktop/bags/test.sh")); //will start new process without blocking
}

MainWindow::~MainWindow(){delete ui;}

How to start a Shell Script with QProcess?

Code is fine. Problem is at run-time.

Either your program can't run /bin/sh for some reason (test if you can run gedit instead?), or the MainDirectory variable has wrong directory path (debug it), or the Shell.sh does not exist in that directory (capitalization mistakes? What about "./Shell.sh"?), or you don't have enough privileges to run or modify target directory/files (are they owned by you?).

qprocess get shell like behaviour

Is there any Qt like way to handle that and force Qt to use some default command line interpreter?

The simple answer is no, there isn't a Qt default command line interpreter

QString command = "grep \"false negatives\" test.txt | cut -f2";

This command doesn't work because QProcess takes the first token (grep) and uses that as the command, then passes each item, separated by a space to that command. In this case, the pipe command is not a valid argument for grep and neither is cut, nor -f2.

I commented that the answer to this question was possibly similar, as it demonstrates how you can successfully use the pipe command with QProcess; note that the arguments are surrounded by quotes.

As you don't want to call cmd or a *nix equivalent such as bash, you can handle this with two calls to QProcess; the first for the grep command and the 2nd for the cut, passing in the output from the first QProcess call.

The function QProcess::setStandardOutputProcess makes this easier, allowing you to create the pipe directly between the two QProcess objects.

Therefore you'd do something like this: -

QProcess proc1;
QProcess proc2;

proc1.setStandardOutputProcess(&process2);

QString cmd1("grep \"false negatives\" test.txt");
QString cmd2("cut -f2");

proc1.start(cmd1);
proc2.start(cmd2);

Cannot execute echo command in QProcess

You can't use the pipe command (|) with QProcess that way.

There are a few ways to tackle this: -

You can call the first command and retrieve its output before processing it either in Qt or with another call to QProcess.

Or, create a script that you call from QProcess and retrieve the output.

Finally, assuming you're using linux / OSX, you can call QProcess with /bin/bash and pass the command to that. For example: -

env = "/bin/bash \"echo TRIG | nc 192.168.1.100 23 -q1\"";
process1.execute(env);

You can probably find an equivalent to /bin/bash for windows, perhaps cmd.exe

Trying to run a shell script from Qt with sudo rights

Not solved as such.

I got round this by

  1. using one of the options that actually runs the script (without su rights), and
  2. granting permission in sudoers, to user, to run the command in the script without password prompt.

hope that makes sense.

not happy, but it works.



Related Topics



Leave a reply



Submit