1

I know that the function:

system("myfile.sh")

exec a bash script. Ok but now I want to redirect the output to my program to ensure the reading. For example the script date.sh give me the date of my system, and i want to see it on my program with std::cout << OUTPUTDATE; Is it possible? How?

1

1 Answer 1

6

Use popen instead of system.

The function popen will give you a FILE * you can read from.

FILE *script = popen("myfile.sh", "r");
while (fgets(line, LENGTH, script)) {
    /* ... */
}
pclose(script);
Sign up to request clarification or add additional context in comments.

2 Comments

With this method, can I send easly and without error the output to a client on my network? Thx
@user840718 With this method you can capture the output of "myfile.sh". I don't know about the clients on your network :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.