I’m trying to call a shell script from C++ with custom input. What I could do is:
void dostuff(string s) {
system("echo " + s + " | myscript.sh");
...
}
Of course, escaping s is quite difficult. Is there a way that I can use s as stdin for myscript.sh? Ie, something like this:
void dostuff(string s) {
FILE *out = stringToFile(s);
system("myscript.sh", out);
}
A simple test to reassign stdin and restore it after the
systemcall:Works out nicely. I tested it with a simple
dummy.shscript like this:Note the last line dumps standard input to standard output, so you could test it like
and expect the following output: