Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quoting and escaping (commandline and args) #384

Open
user706 opened this issue Jul 10, 2024 · 1 comment
Open

quoting and escaping (commandline and args) #384

user706 opened this issue Jul 10, 2024 · 1 comment

Comments

@user706
Copy link

user706 commented Jul 10, 2024

Hi,

how can one get the quoting and escaping right, when one wants to issue complex command-lines?

Here are examples:

a)

/bin/bash -c 'set -Eeuo pipefail ; trap "trap - SIGTERM && echo \"killing -$$\" && kill -- -$$" SIGINT SIGTERM EXIT; echo "pid $$" ; sleep 1; echo "done"'

equiv with the following (but now using only double-quotes)

b)

/bin/bash -c "set -Eeuo pipefail ; trap \"trap - SIGTERM && echo \\\"killing -\$\$\\\" && kill -- -\$\$\" SIGINT SIGTERM EXIT; echo \"pid \$\$\" ; sleep 1; echo \"done\""

equiv with

a2)

eval "/bin/bash -c 'set -Eeuo pipefail ; trap \"trap - SIGTERM && echo \\\"killing -\$\$\\\" && kill -- -\$\$\" SIGINT SIGTERM EXIT; echo \"pid \$\$\" ; sleep 1; echo \"done\"'"

equiv with

b2)

eval "/bin/bash -c \"set -Eeuo pipefail ; trap \\\"trap - SIGTERM && echo \\\\\\\"killing -\\\$\\\$\\\\\\\" && kill -- -\\\$\\\$\\\" SIGINT SIGTERM EXIT; echo \\\"pid \\\$\\\$\\\" ; sleep 1; echo \\\"done\\\"\""

How does one construct those command-lines above, for boost.process??

If I copy/paste the above commands in bash, I get this output:

pid 20906
done
killing -20906
Terminated               // this line (and line above this one) is printed because of EXIT in the command (removing EXIT, will result in TERMINATED not being printed

More easily readable, I basically want to have /bin/bash execute the following:

set -Eeuo pipefail
trap "trap - SIGTERM && echo \"killing -$$\" && kill -- -$$" SIGINT SIGTERM EXIT
echo "pid $$"
sleep 1
echo "done"

Thanks!

@user706
Copy link
Author

user706 commented Jul 10, 2024

Perhaps like this:

#include <boost/process.hpp>

#include <string>
#include <iostream>
#include <chrono>
#include <thread>

int main()
{
    namespace bp = boost::process;
    bp::group g;
//  bp::child c(        "/bin/bash",           "-c",  "set -Eeuo pipefail ; trap \"trap - SIGTERM && echo \\\"killing -$$\\\" && kill -- -$$\" SIGINT SIGTERM EXIT; echo \"pid $$\" ; sleep 1; echo \"done\"" , g);
    bp::child c(bp::exe="/bin/bash", bp::args={"-c",  "set -Eeuo pipefail ; trap \"trap - SIGTERM && echo \\\"killing -$$\\\" && kill -- -$$\" SIGINT SIGTERM EXIT; echo \"pid $$\" ; sleep 1; echo \"done\""}, g);
    //                                                ^^^^^^^...
    //                                                same as b) above; except: here we do not escape $:   so instead of \$   we use $
    
    std::cout << "\tNote: c.id() == " << c.id() << std::endl;
    
    std::this_thread::sleep_for(std::chrono::milliseconds{100});
    //::kill(-c.id(), SIGTERM);
    //::kill(-c.id(), SIGINT);
    
    c.wait();
}

How would one do it in a single string (instead of splitting into "/bin/bash", "-c", "...") ? Is that possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant