简体   繁体   English

在 Windows 上运行带有 C++ 的 bash 脚本

[英]Run a bash script with C++ on Windows

I am trying to write a program in C++ that can execute a bash script on Windows and then read the output of the bash script and store it into a string or something like that. I am trying to write a program in C++ that can execute a bash script on Windows and then read the output of the bash script and store it into a string or something like that. Is this even possible without installing any extra software on Windows?如果不在 Windows 上安装任何额外的软件,这甚至可能吗? If so, how?如果是这样,怎么做?

Also, would it work if I wrote the program on Linux with a Posix library and then cross-compiled the C++ program for Windows inside Linux and then move it over to Windows where it needs to execute the bash script? Also, would it work if I wrote the program on Linux with a Posix library and then cross-compiled the C++ program for Windows inside Linux and then move it over to Windows where it needs to execute the bash script?

You can use the popen function.您可以使用popen function。

FILE *fp;
fp = popen("bash script.sh", "r");

Now you can read the output just like you would read a file.现在您可以像读取文件一样读取 output。 Example:例子:

char output[100];
fgets(output, sizeof(output), fp);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM