execlp 多个“程序";

2022-01-23 00:00:00 command bash pipe os.execl c++

我想运行类似的东西

cat file.tar | base64 | myprogram -c "| base64 -d | tar -zvt "

我使用 execlp 来运行进程.

I use execlp to run the process.

当我尝试运行类似 cat 的东西时,它可以工作,但如果我尝试运行 base64 -d |tar -zvt 不起作用.

When i try to run something like cat it works, but if i try to run base64 -d | tar -zvt it doesn't work.

我查看了 bash 命令,发现我可以运行 bash 并告诉他运行其他程序.所以它是这样的:

I looked at the bash commands and I found out that I can run bash and tell him to run other programs. So it's something like:

execlp ("bash", "-c", "base64 -d | tar -zvt", NULL);

如果我在终端上运行它,它运行良好,但使用 execlp 它不起作用.如果我使用 execlp("cat", "cat", NULL) 它可以工作.

If I run it on the terminal, it works well, but using the execlp it dont work. If I use execlp("cat", "cat", NULL) it works.

有人知道如何使用 execlp 上的 -c 参数来执行多个程序"吗?我不能使用系统,因为我使用管道和叉子.

Someone knows how to use the -c param on execlp to execute multiple "programs"? I cant use system because i use pipe and fork.

现在我注意到,如果我尝试使用 execlp("bash", "bash", "-c", "base64", NULL)... 没有任何反应.如果我使用 execlp("cat", NULL) 没关系..我正在写信给标准输入......我不知道它是否是 bash -c base64 的问题......因为如果我在终端上运行 echo "asd" |bash -c猫"一切顺利

Now i noticed, if i try to use execlp("bash", "bash", "-c", "base64", NULL)... nothing happens. If i use execlp("cat", NULL) it's ok.. I'm writing to the stdin... i don't know if its the problem with the bash -c base64.. because if i run on the terminal echo "asd" | bash -c "cat" it goes well

推荐答案

第一个参数"变成了 argv[0],所以你应该用类似这样的方式调用:

The first "argument" is what becomes argv[0], so you should call with something like:

execlp("bash", "bash", "-c", "base64 -d | tar -zvt", NULL);

编辑 上面的函数做了什么的小解释:exec 系列函数执行程序.在上面的调用中,有问题的程序是bash"(第一个参数).Bash 和所有其他程序一样,有一个 main 函数,它是程序的起点.和所有其他 main 函数一样,Bash 中的函数接收两个参数,通常称为 argcargv.argv 是一个以零结尾的字符串数组,argcargv 数组中的条目数.argc 将始终至少为 1,这意味着 argv[0] 处始终存在一个条目.第一个条目是程序的名称",通常是程序文件的路径.命令行上的所有其他程序参数都放入 argv[1]argv[argc - 1].

Edit A small explanation what the above function does: The exec family of functions executes a program. In the above call the program in question is "bash" (first argument). Bash, like all other programs, have a main function that is the starting point of the program. And like all other main functions, the one in Bash receives two arguments, commonly called argc and argv. argv is an array of zero-terminated strings, and argc is the number of entries in the argv array. argc will always be at least 1, meaning that there is always one entry at argv[0]. This first entry is the "name" of the program, most often the path of the program file. All other program arguments on the command line is put into argv[1] to argv[argc - 1].

execlp 的作用是使用第一个参数找到要执行的程序,所有其他参数将按照它们的顺序放入programs argv 数组中给出.这意味着上面对 execlp 的调用将调用程序bash"并将 Bash 的 argv 数组设置为:

What execlp does is use the first argument to find the program to be executed, and all the other arguments will be put into the programs argv array in the order they are given. This means that the above call to execlp will call the program "bash" and set the argv array of Bash to this:

argv[0] = "bash"
argv[1] = "-c"
argv[2] = "base64 -d | tar -zvt"

另外,Bash 的 argc 将被设置为 3.

Also, argc of Bash will be set to 3.

如果第二个"bash"改为"foo",那么Bash的argv[0]会被设置为foo" 也是如此.

If the second "bash" is changed to "foo", then argv[0] of Bash will be set to "foo" as well.

我希望这能澄清一点.

相关文章