July 1, 2007
How can I run a linux bash command in my C++ program?
Hi dear,
- I wanna write a C++ program in Linux environment which is able to run a Linux command ( e.g. ls) while running.
- Check out the man pages for system(3), fork(2) and exec(3).
- How can I do this?
- There's two ways you can do it, depending on how comfortable you are with C/C++ programming on Linux.
1. Use the system() function, passing to it a string containing the command you want to run.
eg. system("ls -l");
2. Use fork() to create a child process, and have the child process call execve() to run the command.Which one you use depends also depends on whether you want your program to wait until the command executes or whether it should continue running.























