c++11boostboost-process

can boost::process::system support bash commands separated by a semicolon?


Code:

#include <boost/system/process.hpp>  
int main() {  
  boost::process::system bp("echo foo; echo bar");  
}  

Output:
foo; echo bar

Desired output:
foo
bar

I am receiving a string containing 1:MANY commands to run separated by a semi-colon; as one may run in in a shell terminal. Is there a way to tell boost::process to interpret the string command as such?


Solution

  • Yes, I'd use an explicit bash:

    Live On Coliru

    #include <boost/process.hpp>
    namespace bp = boost::process;
    
    int main() {
        bp::child c(
            bp::search_path("bash"),
            std::vector<std::string>{
                "-c", "echo foo; for a in {1..10}; do echo \"bar $a\"; done"});
        c.wait();
    }
    

    Prints

    foo
    bar 1
    bar 2
    bar 3
    bar 4
    bar 5
    bar 6
    bar 7
    bar 8
    bar 9
    bar 10