Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从C++中执行BASH脚本并读取其输出Ubuntu SDK_C++_Linux_Bash_Ubuntu_Qml - Fatal编程技术网

从C++中执行BASH脚本并读取其输出Ubuntu SDK

从C++中执行BASH脚本并读取其输出Ubuntu SDK,c++,linux,bash,ubuntu,qml,C++,Linux,Bash,Ubuntu,Qml,我有一个ubuntu应用程序,我正试图从中执行bash脚本,但它似乎不起作用。我试着用这个系统 #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(int argc, char *argv[]) { // tried both system("./script.sh"); // system ("scrip

我有一个ubuntu应用程序,我正试图从中执行bash脚本,但它似乎不起作用。我试着用这个系统

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>

    int main(int argc, char *argv[])
    {
// tried both
        system("./script.sh");
       // system ("script.sh")

    }
另外,我也尝试过研究这个问题,但没有找到解决办法;也可以读取输出并在文本框中显示。

使用popen


运行脚本与此无关。这将适用于任何shell命令。

对于任何希望在QT中执行此操作的人,我所做的如下:

 QProcess proc;

        proc.start("gnome-terminal", QIODevice::ReadWrite);

        if (proc.waitForStarted() == false) {
            qDebug() << "Error starting terminal process";
            qDebug() << proc.errorString();

            return (-1);
        }

这是一个标准的POSIX函数,它不依赖于任何特殊的库。它读取脚本写入其标准输出的任何内容。
 QProcess proc;

        proc.start("gnome-terminal", QIODevice::ReadWrite);

        if (proc.waitForStarted() == false) {
            qDebug() << "Error starting terminal process";
            qDebug() << proc.errorString();

            return (-1);
        }