execlp多个"程序" [英] execlp multiple "programs"
问题描述
我想运行像
cat file.tar | base64 | myprogram -c "| base64 -d | tar -zvt "
我用 execlp
来运行该进程。
当我尝试运行像猫
它的工作原理,但如果我尝试运行的base64 -d |焦油-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(猫,猫,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.
有人知道如何使用 -c
参数上execlp执行多个计划?
因为我用管和叉我不能使用的系统。
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(庆典,庆典,-c,BASE64,NULL)...没有任何反应。
如果我使用execlp(猫,NULL)这是确定..
我写的标准输入...我不知道,如果它与在bash -c的base64 ..的问题,因为如果我在终端上运行的回声ASD|庆典-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
系列函数执行程序。在上面的电话有问题的程序是喂(第一个参数)。击,像所有其他程序,有一个主
函数是程序的起点。像所有其他主
函数,一个在猛砸接收两个参数,通常被称为 ARGC
和的argv
。 的argv
是零结尾的字符串数组,而 ARGC
的条目在的argv
阵列。 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
确实是使用的第一个参数找到该程序被执行,所有的其他参数将投入节目的argv在订单
阵列给予他们。这意味着,到 execlp
上面的调用会调用程序庆典和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"
此外, ARGC
猛砸将被设置为3。
如果第二个庆典
更改为foo的
,那么的argv [0]猛砸
将被设置为foo的
以及
If the second "bash"
is changed to "foo"
, then argv[0]
of Bash will be set to "foo"
as well.
我希望这清除它一点点。
I hope this clears it up a little bit.
这篇关于execlp多个"程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!