July 1, 2007

How can I run a linux bash command in my C++ program?

Hi dear,

  1. I wanna write a C++ program in Linux environment which is able to run a Linux command ( e.g. ls) while running.
  2. Check out the man pages for system(3), fork(2) and exec(3).
  3. How can I do this?
  4. 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.

Tags: ,

Tell a Friend Today!

del.icio.us Digg Furl Reddit Ask BlinkList blogmarks Blogg-Buzz Google Ma.gnolia Netscape ppnow Rojo Shadows Simpy Socializer Spurl StumbleUpon Tailrank Technorati Windows Live Wists Yahoo! Help

Permalink • Print

Track this entry

RSS BlogPulse

RSS Technorati Cosmos

Related Searches

, , , ,