简体   繁体   中英

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. Is this even possible without installing any extra software on 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?

You can use the popen function.

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

Now you can read the output just like you would read a file. Example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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