PDA

View Full Version : how to use sudo command in Qprocess



iswaryasenthilkumar
23rd June 2015, 09:06
i want to execute this shell command in qt

sudo ./filename.sh

this code executng in terminal,,while in Qprocess i getting error

/bin/sh: 0: Can't open sudo

my code below:


QProcess process;
bool x;
qint8 status;
x=process.startDetached("/bin/sh", QStringList()<< "sudo "<<"./samp.sh");
qDebug()<<process.readAllStandardOutput();
status=process.waitForFinished();
if(!x)
{
qDebug()<<process.errorString();
}
else
{
qDebug()<<"success";
}
after searching i got one solution for sudo which wysota said
http://www.qtcentre.org/threads/39621-QProcess-unable-to-start-shell-commands-linux

QString apath,program;
QStringList args;
apath=QDir("/bin/bash").absolutePath());
program="gksudo";//i used gksudo i getting no answer
args << "apt-get install";
process->start(program,args);

how to use sudo commang in Qprocess please give me suggestion for this:(
Thanks in advance:)

anda_skoa
23rd June 2015, 10:11
i want to execute this shell command in qt

sudo ./filename.sh


If this is the command you want to execute, why is your startDetached() executing /bin/sh?





x=process.startDetached("/bin/sh", QStringList()<< "sudo "<<"./samp.sh");
qDebug()<<process.readAllStandardOutput();
status=process.waitForFinished();


I would recomend to have a look at the documentation of QProcess, especially the section startDetached() is in (static public members).
You should then come to the right conclusion why this code above will never do what you seem to expect it would.

Cheers,
_

iswaryasenthilkumar
24th June 2015, 07:55
thanks anda_skoa ..i read qprocess documentation and understand my mistake.and i changed my code,
p

bool success;
QProcess gunzip;
QStringList arguments;
gunzip.setProcessChannelMode(QProcess::MergedChann els);
QString program = "gksudo";
arguments<<"./omxtx.sh";
gunzip.start(program, arguments);
success = gunzip.waitForStarted();
if (!success)
{
qDebug("waitForStarted() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
success = gunzip.waitForFinished();
if (!success)
{
qDebug("waitForFinished() error: %s", gunzip.errorString().toUtf8().constData());
return;
}
QByteArray buffer = gunzip.readAll();
QString output = buffer;
qDebug("%s", output.toUtf8().constData());


the above code executing but i dont get proper result it getting password from user after that it not showing the the file output actualy inside the file i wrote echo hello i need output as hello i dont get this result i used this command "gksudo ./omxtx.sh" in terminal it get password and showing hello but in qt it getting password but it not showing hello what mistake am doing please guide me to get proper result
[QUOTE=iswaryasenthilkumar;278540]i want to execute this shell command in qt

sudo ./filename.sh

If this is the command you want to execute, why is your startDetached() executing /bin/sh?


I would recomend to have a look at the documentation of QProcess, especially the section startDetached() is in (static public members).
You should then come to the right conclusion why this code above will never do what you seem to expect it would.

Cheers,
_

anda_skoa
24th June 2015, 10:01
Have you verified that the omxtx.sh is actually being executed?
Does it do what it is supposed to do and you are just not getting its output?

Cheers,
_

iswaryasenthilkumar
24th June 2015, 11:10
yes it executing in terminal i getting output "hello".bt in Qprocess am not getting that
Have you verified that the omxtx.sh is actually being executed?
Does it do what it is supposed to do and you are just not getting its output?

Cheers,
_

anda_skoa
24th June 2015, 13:14
yes it executing in terminal i getting output "hello".bt in Qprocess am not getting that
I am not sure what the relevance of that would be.

You already stated that it works if called manually, so that is obviously not what I meant when I wrote whether you verified that the script got executed.

But to make it more clear: check that the script actually gets executed when you run gksudo through QProcess.
I.e. to determine whether your problem with then output is the script not running or the text not being visible to QProcess.

Cheers,
_