简体   繁体   中英

how to get the echo statements as well as execution status from a shell script into a c++ program

I have a shell script like:

#!/bin/bash
echo "my string"
return 1

And I have a c++ code where I want both the echo statement(s) as well as the exec status. I found some examples where we can use popen() or system() both the ways serves one requirement, but not both. Any suggestions how to do this?

fork() + exec() would be a pretty standard way to run another executable ( please don't ever use system() , it's a security nightmare ). To obtain the standard out / standard error stream output from the spawned process, the parent process would set up pipe s to connect to those streams and read them. As for the exit code, that you can get via waitpid() once the child process terminates (you'll get a SIG_CHILD signal, so make sure you have installed a signal handler for that).

I recommend a read through of Advanced Programming in the UNIX Environment, 3rd Edition .

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