从c++程序运行shell脚本会自动将shell脚本的输出显示到控制台吗
Will running a shell script from a c++ program automatically display the output of the shell script to the console?
我正在学习一个Linux类,以前从未学习过c++。我的shell脚本基本上都是自己完成的,但我需要从c++程序中使用它。当我的shell脚本从命令行运行时,它会将我想要返回的结果输出到控制台。当我从c++程序运行它时,我的程序会编译并运行,但我没有得到任何输出。这是因为我的c++程序中出现了一些错误,还是因为c++和shell脚本的交互方式?
我看到了一些关于从shell脚本获取输出并在c++程序中使用它的问题,但我不想这么做。实际上,我的c++程序所做的就是运行shell脚本。
我只想把shell脚本的输出显示在控制台上。你能帮忙吗?如果需要,我可以发布我正在使用的代码。
C++:
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main (int argc, char *argv[]) {
string arg;
arg = string(argv[1]);
if (argc >= 2) {
for (int i=2; i < argc; i++) {
string temp = string(argv[i]);
arg=arg+" "+temp;
}
}
string command;
command = "./findName.sh "+ arg;
//cout << command;
system("command");
return 0;
}
如果你想实时显示输出,你可以使用这样的东西:
FILE* outputStream;
char buffer[1024];
outputStream = popen(command,"r");
if (outputStream == NULL)
return 1; //couldn't execute command
while (fgets(buffer, sizeof(buffer), outputStream) != NULL) {
//read output line-by-line to buffer and display it
std::cout << buffer << std::endl;
}
pclose(outputStream);
相关文章:
- 如何通过cpp程序运行shell脚本
- 我适合在我的构建系统中使用 shell 脚本而不是AX_CHECK_COMPILE_FLAG吗?
- CMake 引用我在 ~/bin 中创建的随机 shell 脚本(即使我删除了它)?
- 从带有参数的C++程序中调用shell脚本
- 通过C++应用程序运行 shell 脚本始于 JAVA
- 当 PHP、C++ 和 shell 脚本尝试访问同一个文件时,如何避免错误?
- 在 c++ 中捕获 shell 脚本退出状态
- 如何将我的 cli 信息(变量等)共享到子进程,这是一个 bash shell 脚本
- 为什么 C++ 中的 shell 脚本代码不起作用?
- GDB 在 shell 启动时加载找不到的脚本
- C++:在Linux shell脚本中运行GDB
- C++中的程序如何使用"系统"变量与 Linux 中的 shell 脚本交互?
- 并行gnu通过shell脚本传递参数
- 将参数从 Qt 传递到 shell 脚本文件
- 运行 shell 脚本时出错:"pipe call failed"
- 从c++程序运行shell脚本会自动将shell脚本的输出显示到控制台吗
- 从shell脚本运行c scrips
- C 程序需要STDIN的输入,但我想从Shell脚本中输送/发送这些输入
- 如何在C++中正确运行 shell 脚本
- Shell脚本终止程序,但导致无法写入输出文件