bash 如何使用 QProcess 启动 Shell 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9086771/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:27:31  来源:igfitidea点击:

How to start a Shell Script with QProcess?

c++qtbashshellqprocess

提问by Streight

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

如何使用 QProcess 启动 Shell 脚本?Shell 脚本中有八个不同的命令,其中一些带有参数,另一些则没有。

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

我尝试使用(使用 Ubuntu 11.10)启动 Shell 脚本:

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?

但这不起作用,这意味着什么也没有发生。如何使它工作?

采纳答案by Hossein

Code is fine. Problem is at run-time.

代码没问题。问题出在运行时。

Either your program can't run /bin/shfor some reason (test if you can run geditinstead?), or the MainDirectoryvariable has wrong directory path (debug it), or the Shell.shdoes 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?).

您的程序/bin/sh由于某种原因无法运行(测试您是否可以运行gedit?),或者MainDirectory变量的目录路径错误(调试它),或者该Shell.sh目录中不存在该目录(大写错误?“./Shell 怎么样?” .sh”?),或者您没有足够的权限来运行或修改目标目录/文件(它们是否归您所有?)。

回答by Neox

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 readyReadStandardOutput()或/和void readyReadStandardError()并显式地从进程中读取。例如:

void onReadyRead() {

   QByteArray processOutput = Prozess->readAllStandardOutput();
}

回答by Denzil

This should work:

这应该有效:

QProcess::ProcessError Error = myProcess->readAllStandardError();
return Error;