6

How can I start a Shell Script using QProcess? The Shell Script has eight different commands in it, some with arguments others without.

I tried to start the Shell Script with (using Ubuntu 11.10):

QProcess *Prozess = new QProcess();
Prozess->setWorkingDirectory(MainDirectory);
Prozess->start("/bin/sh", QStringList() << "Shell.sh");

But this doesn't work, that means nothing happens. How to make it work?

Hossein
  • 4,097
  • 2
  • 24
  • 46
Streight
  • 811
  • 4
  • 15
  • 28
  • 2
    The code is fine. It must be running the program in the background without showing the terminal window. – Hossein Jan 31 '12 at 21:38

4 Answers4

5

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?).

Hossein
  • 4,097
  • 2
  • 24
  • 46
  • hi, great idea :). gedit works excellent! problem is I need "/bin/sh" to work. "./Shell.sh" did not change anything. I can't vote up yet since I have not 15 reputation. Any ideas how to make "/bin/sh" work? – Streight Feb 01 '12 at 13:43
2

The process you have started is running in background. if you want to see any explicit output from the running script you have to connect to void readyReadStandardOutput() or/and void readyReadStandardError() and read from the process explicitly. For example:

void onReadyRead() {

   QByteArray processOutput = Prozess->readAllStandardOutput();
}
Neox
  • 2,000
  • 13
  • 12
  • The system surveillance does not show any process running in background and the files the Script should create are not created. So "Prozess->readAllStandardOutput();" also did not help. The Shell Script should create the files by execute it with "sh". – Streight Jan 31 '12 at 23:18
0

This should work:

QProcess::ProcessError Error = myProcess->readAllStandardError();
return Error;
Denzil
  • 326
  • 3
  • 15
0
QProcess ().execute ("/bin/sh " + MainDirectory + "/Shell.sh");

will do the job.