0

I am trying to pass a command into my shell script via a C++ program, but I am not familiar with C++ at all and, while I know that I must use system(), I am not sure how to set it up effectively.

#include <iostream>
#include <stdlib.h>

int main() {
        system("./script $1");

        return 0;

}

This is what I currently have.

It seems that I can't use positional parameters in the system command, but I wasn't sure what else to do. I'm trying to pass in an argument to the script via the C++ program.

3 Answers 3

4

If you just want to call "./script" with the first argument to the C++ program passed as the first argument to the script, you could do it like this:

#include <iostream>
#include <string>
#include <stdlib.h>

int main(int argc, char ** argv)
{  
   if (argc < 2)
   {  
      printf("Usage:  ./MyProgram the_argument\n");
      exit(10);
   }

   std::string commandLine = "./script ";
   commandLine += argv[1];

   std::cout << "Executing command: " << commandLine << std::endl;
   system(commandLine.c_str());
   return 0;
}
Sign up to request clarification or add additional context in comments.

5 Comments

This seems to work, however I am still not sure how I would reference the positional parameter in linux, if that makes any sense.
@rjoor are you asking how to reference the positional parameter to the script within the script?
@JohnFilleau I am asking how to reference it in the actual C++ program. I am able to reference it within the script as $1, but that doesn't seem to work when I do it in the C++ program.
The two valid signatures for main in a C++ program are int main() and int main(int argc, char** argv). argc stands for "argument count" it's the number of command line arguments used to invoke your program, including the program name itself. argv stands for "argument values" and is a pointer to an array of c-strings that are each a single command line argument. argv[0] is your program name. argv[1] is the first argument to your C++ program. argv[2] is the second argument, etc.
@JohnFilleau Thanks, I got it figured out!
1

Properly executing a shell command from C++ actually takes quite a bit of setup, and understanding exactly how it works requires a lot of explanation about operating systems and how they handle processes. If you want to understand it better, I recommend reading the man pages on the fork() and exec() commands.

For the purposes of just executing a shell process from a C++ program, you will want to do something like so:

#include <unistd.h>
#include <iostream>

int main() {
    int pid = fork();
    if (pid == 0) {
        /*
         * A return value of 0 means this is the child process that we will use
         * to execute the shell command.
         */
        execl("/path/to/bash/binary", "bash", "args", "to", "pass", "in");
    }

    /*
     * If execution reaches this point, you're in the parent process
     * and can go about doing whatever else you wanted to do in your program.
     */
    std::cout << "QED" << std::endl;
}

To (very) quickly explain what's going on here, the fork() command essentially duplicates the entire C++ program being executed (called a process), but with a different value of pid which is returned from fork(). If pid == 0, then we are currently in the child process; otherwise, we're in the parent process. Since we're in the dispensable child process, we call the execl() command which completely replaces the child process with the shell command you want to execute. The first argument after the path needs to be the filename of the binary, and after that you can pass in as many arguments as you want as null-terminated C strings.

I hope this helps, and please let me know if you need further clarification.

2 Comments

This also seemed to work, the only issue I have is that the shell script claims to receiving more than one command line argument (it is only designed for 1). This is strange to me considering I only included the path and the $1 parameter in the execl call.
Hey there - sorry for the late reply, but I just remembered a critical piece of info for running execl(): the last argument must be nullptr to indicate the end of the argument list.
0

the problem I have understood so far is that you want to pass arguments to c++ executable and it will then pass those arguments further to the system script

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

// argc is the count of the arguments
// args is the array of string (basically the arguments)

int main(int argc, char ** argv) {

   // Convetion to check if arguments were provided
   // First one or two are the not positional. (these are information related to the binary that is currently being executed)
   if (argc < 2) {
      printf("Please Provide an Argument");
      return 100; // Any number other than 0 (to represent abnormal behaviour)
   }

   string scriptCommand = "./name-of-script"; // script in your case

   // Loop through and add all the arguments.
   for (int i = 1; i < argc; i++)
      scriptCommand += " " + argv[i];

   system(scriptCommand.c_str());
   return 0; // Represents a normal exit (execution).
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.