I am trying to run a small bash script inside a QT application which contains an if-test-then block and the necessary quotes inside the test. As the script should be compiled by QT and run by bash, the quotes have to be escaped twice, so the escaping backslashes for the quotes have to be escaped also, as I see it. Unfortunately it does not work as expected.
Here is the code:
#include <stdio.h>
QObject *parent;
int main(int argc, const char* argv[])
{
QProcess *myProcess = new QProcess(parent);
myProcess->execute("/bin/bash -c \"x=1 ; echo $x ; if [ \\\"$x\\\" = \\\"1\\\" ] ; then echo itsOne ; fi\"");
}
root@debian:~# ./proggy
1
/bin/bash: line 0: [: missing `]'
root@debian:~# x=1 ; echo $x ; if [ "$x" = "1" ] ; then echo itsOne ; fi
1
itsOne
A couple of points.
Firstly execute is a static member of QProcess so there's no need to create an instance of QProcess.
Secondly, it's generally easier to use the execute overload that separates the program name from the argument list.
With that in mind what you want is probably something like...
QProcess::execute("/bin/bash", QStringList() << "-c" << "x=1 ; echo $x ; if [ \\\"$x\\\" = \\\"1\\\" ] ; then echo itsOne ; fi");