PDA

View Full Version : Running a bash script from resource (qrc) file



onamatic
4th August 2011, 15:40
Linux / Qt4.6.3
I want to run a shell script which I've placed in a .qrc resource.



...
QProcess qp;
QString script(":/scripts/findttyusb.sh"); //<-- qp.waitForFinished() returns false (error)
QString script("/root/Projects/UT804/UT804/scripts/findttyusb.sh"); //<--- works fine
QProcess::
qp.start(script);
if (qp.waitForFinished()){
if (qp.exitCode() == 0){
QByteArray result = qp.readAll();
...


Do I have to explicitly get the script out of the qrc, create a file and then execute it - or is there a really easy way to do this?
Thanks for any thoughts.

nightghost
4th August 2011, 15:49
I don't think, that this is possible, because resources are compilied into the binary.

yeye_olive
4th August 2011, 15:51
I suppose you could start bash in interactive mode and dump the script on its standard input, but this would be ugly, error-prone, and you would have to use the native API since QProcess only allows you to redirect the standard input to a file, not an arbitrary QIODevice.

onamatic
4th August 2011, 21:14
Thanks for the thoughts people.

I suspect I'm better off just running an external script as normal. In the back of my mind I seem to remember doing something similar with Qt a few years ago; but then, I'm getting old.

spark82
3rd February 2012, 10:09
I suppose you could start bash in interactive mode and dump the script on its standard input, but this would be ugly, error-prone, and you would have to use the native API since QProcess only allows you to redirect the standard input to a file, not an arbitrary QIODevice.

Perhaps ugly, but that seams to work:



QProcess *proc = new QProcess;
QString name = "/bin/bash";
QStringList arg;
arg << "-c" ;

QFile file(":/test.sh");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return 42;

arg << file.readAll();
proc->start(name, arg);