Sourcing a script using QProcess
-
Hi! I want to execute a bash script from my application using QProcess. Because the script exports some env vars I need to "source" it. Example:
cat myscript.sh #!/bin/sh echo Hello World export MYVAR=blablabla
here my Qt application:
QProcess p; p.start("myscript.sh");
executes the script, but of course no vars are exported to the current shell. But:
p.start("source myscript.sh");
doesn't execute the script at all, despite no errors are returned. I said "doesn't execute" because waitForStarted() fails and "Hello world" isn't echoed.
Where is my mistake?
-
Hi
is source a standalone program/command ?
else it wont work as QProcess
have no idea how to run it
if internal bash command.
( I think )
maybe:
Process.start("bash", QStringList() << "-c" << "source" << "myscript.sh "); -
Hi,
I haven't tested source with QProcess but in any case the correct call would rather be:
p.start("source", QStringList() << "/path/to/myscript.sh");
Give the full path to myscript.sh. You are likely using a shadow build so myscript.sh will not be in the same folder as your application.
-
Try with
p.start("bash", QStringList() << "-c" << "\"\"source /path/to/test.sh\"\"");
-
How are you checking that it does nothing with QProcess ?
In the console version you have too much quotes it should only be
bash -c "source /path/to/test.sh"
-
How are you checking that it does nothing with QProcess ?
The env vars are not exported and there is no echo.
In the console version you have too much quotes it should only be bash -c "source /path/to/test.sh"
Yes, in this way the echo is shown but the vars are not exported. It's the same as run the script directly without sourcing it. But it's beyond my knowledge to understand why.
-
On OS X putting the echo call inside the "command string" worked.
I'm reading stdout using readAllStandardOutput
-
You can set the environment using
QProcess::setProcessEnvironment(const QProcessEnvironment & environment)
You can get the environment of your current process via
QProcessEnvironment::systemEnvironment()