shell commands and c++

Discussion in 'Mac Programming' started by widgetman, Oct 7, 2004.

  1. macrumors member

    Joined:
    Oct 7, 2004
    #1
    im really new at c++, but i know some very basic commands. i want to know how to run shell commands and return the result.

    thank you.
     
  2. Moderator emeritus

    Joined:
    Jun 25, 2002
    Location:
    Gone but not forgotten.
    #2
    In C, you normally use various exec* or fork* functions to run external programmes. You should be able to use them within C++ to call the various shells.
     
  3. thread starter macrumors member

    Joined:
    Oct 7, 2004
    #3
    are there any special includes i would need?
     
  4. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #4
    For a simple shell script, system(3) is often all you need. Check the manual pages (man system in this case) in the SYNOPSIS section to see what #includes you need (just unistd.h in this case).

    Note that system() always wraps /bin/sh around your command.

    Code:
    #include <iostream>
    #include <unistd.h>
    
    int
    main()
    {
        int rv = system("ls -l ~/");
        std::cout << "result code: " << rv << "\n";
        return 0;
    }
    
    Here's an example of doing it the hard way.
    Code:
    #include <iostream>
    #include <cstdio>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/wait.h>
    
    int
    main()
    {
        int kidstatus, deadpid;
    
        pid_t kidpid = fork();
        if (kidpid == -1) {
            std::cerr << "fork error " << errno << ", "
                      << std::strerror(errno) << "\n";
            return 1;
        }
        if (kidpid == 0) {
            // okay, we're the child process. Let's transfer control
            // to the ls program.  "ls" in both of the spots in the list
            // is not a mistake!  Only the second argument on are argv.
            // note the NULL at the end of the list, you need that.
    #if 0
            // this version uses the shell.
            int rv = execlp("/bin/sh", "/bin/sh", "-c", "ls -l /var/log", NULL);
    #else
            // this version runs /bin/ls directly.
            int rv = execlp("/bin/ls", "/bin/ls", "-l", "/var/log/", NULL);
    #endif
            // if the execlp is successful we never get here:
            if (rv == -1) {
                std::cerr << "execlp error " << errno << ", "
                          << std::strerror(errno) << "\n";
                return 99;
            }
            return 0;
        }
        // we only get here if we're the parent process.
        deadpid = waitpid(kidpid, &kidstatus, 0);
        if (deadpid == -1) {
            std::cerr << "waitpid error " << errno << ", "
                      << std::strerror(errno) << "\n";
            return 1;
        }
        std::cout << "child result code: " << WEXITSTATUS(kidstatus)
                  << "\n";
        return 0;
    }
    
     
  5. thread starter macrumors member

    Joined:
    Oct 7, 2004
    #5
    thanks!!
     
  6. macrumors 6502

    Joined:
    Mar 8, 2004
    Location:
    Here on My Mac(s) in my house in the USA!
    #6
    Thanks this helps me too!!!
     

Share This Page