简体   繁体   English

通过 C++ 程序将命令传递到 shell 脚本

[英]Passing a command into a shell script through a C++ program

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.我试图通过 C++ 程序将命令传递到我的 shell 脚本中,但我根本不熟悉 C++,虽然我知道我必须使用 system(),但我不确定如何有效地设置它。

#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.我正在尝试通过 C++ 程序将参数传递给脚本。

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:如果您只是想调用“./script”并将 C++ 程序的第一个参数作为第一个参数传递给脚本,您可以这样做:

#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;
}

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到目前为止我理解的问题是你想将 arguments 传递给 c++ 可执行文件,然后它会将那些 arguments 进一步传递给系统脚本

#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).
}

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.正确执行来自 C++ 的 shell 命令实际上需要相当多的设置,而要准确理解它是如何工作的,需要对操作系统及其处理进程的方式进行大量解释。 If you want to understand it better, I recommend reading the man pages on the fork() and exec() commands.如果您想更好地理解它,我建议您阅读有关fork()exec()命令的手册页。

For the purposes of just executing a shell process from a C++ program, you will want to do something like so:为了仅从 C++ 程序执行 shell 进程,您需要执行如下操作:

#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() .为了(非常)快速地解释这里发生了什么, fork()命令基本上复制了正在执行的整个 C++ 程序(称为process ),但具有从fork()返回的不同pid值。 If pid == 0 , then we are currently in the child process;如果pid == 0 ,那么我们当前在子进程中; 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.由于我们处于可有可无的子进程中,因此我们调用execl()命令,该命令将子进程完全替换为您要执行的 shell 命令。 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.路径后的第一个参数需要是二进制文件的文件名,之后您可以传入任意数量的 arguments 作为以 null 结尾的 C 字符串。

I hope this helps, and please let me know if you need further clarification.我希望这会有所帮助,如果您需要进一步说明,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM