我不明白怎么execlp()在Linux中运行 [英] I do not understand how execlp() works in Linux
问题描述
我花了最后2天试图理解 execlp()
系统调用,但还没有我在这里。让我直去的问题。
I have spent the last 2 days trying to understand the execlp()
system call, but yet here I am. Let me get straight to the issue.
的手册页
execlp的声明系统调用为 INT execlp(为const char *文件,为const char *阿根廷,...);
与描述:的常量字符精氨酸和随后的椭圆在execl的(),execlp(),和execle()函数可以被认为是为arg0,ARG1,...,ARGN 的
The man page
of execlp declares the system call as int execlp(const char *file, const char *arg, ...);
with the description: The const char arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, ..., argn.
然而,我看到了系统调用被称为像这样在我们的教材: execlp(/ bin / sh的,...,ls -l命令/斌/ ??.. );
(以下简称......是我们要弄清楚的学生)。但是这个系统调用doesn't甚至像什么像手册页
系统调用的声明。
Yet I see the system call being called like this in our text book: execlp("/bin/sh", ..., "ls -l /bin/??", ...);
(the "..." are for us to figure out as students). However this system call doesn´t even resemble anything like the declaration on the man page
of the system call.
我超困惑。任何帮助是AP preciated。
I am super confused. Any help is appreciated.
先谢谢了。
推荐答案
这个原型:
int execlp(const char *file, const char *arg, ...);
说的execlp是一个可变参数的功能。这需要2 为const char *
。自变量的休息,如果有的话,是额外的参数交给程序,我们希望运行 - 也的char *
- 所有这些都是C字符串(最后一个参数必须是一个空指针)
Says that execlp ìs a variable argument function. It takes 2 const char *
. The rest of the arguments, if any, are the additional arguments to hand over to program we want to run - also char *
- all these are C strings (and the last argument must be a NULL pointer)
因此,文件
参数是要执行的可执行文件的路径名。 ARG
是我们要显示为的argv [0]
在可执行文件中的字符串。按照惯例,的argv [0]
仅仅是可执行文件的文件名,通常它被设置为相同的文件
So, the file
argument is the path name of an executable file to be executed. arg
is the string we want to appear as argv[0]
in the executable. By convention, argv[0]
is just the file name of the executable, normally it's set to the same as file
.
的 ...
现在是额外的参数给可执行文件。
The ...
are now the additional arguments to give to the executable.
假设你从一个命令/ shell运行这样的:
Say you run this from a commandline/shell:
$ ls
这会是 execlp(LS,LS,(字符*)NULL);
或者,如果您运行
That'd be execlp("ls", "ls", (char *)NULL);
Or if you run
$ ls -l /
这会是 execlp(LS,LS,-l,/,(字符*)NULL);
所以到 execlp(/ bin / sh的,...,ls -l命令/斌/ ??,...);
在这里,你要外壳,/ bin / sh的,而你给的shell命令来执行。这命令是ls -l命令/斌/ ??。您可以从一个命令/ shell手动运行:
Here you are going to the shell, /bin/sh , and you're giving the shell a command to execute. That command is "ls -l /bin/??". You can run that manually from a commandline/shell:
$ ls -l /bin/??
现在,你怎么运行shell,并告诉它执行命令?你打开的文档/手册页的外壳和阅读。
Now, how do you run a shell and tell it to execute a command ? You open up the documentation/man page for your shell and read it.
要运行的是:
$ /bin/sh -c "ls -l /bin/??"
这成为
execlp("/bin/sh","/bin/sh", "-c", "ls -l /bin/??", (char *)NULL);
附注:
在 /斌/ ??
正在做模式匹配,这种匹配由壳完成,/它扩展到所有文件下的/ bin中有2个字符。如果你压根儿
Side note:
The /bin/??
is doing pattern matching, this pattern matching is done by the shell, and it expands to all files under /bin/ with 2 characters. If you simply did
execlp("ls","ls", "-l", "/bin/??", (char *)NULL);
也许什么都不会发生(除非有一个实际文件名为 /斌/ ??
)因为没有外壳,除$ P $点和扩展/斌/?
Probably nothing would happen (unless there's a file actually named /bin/??
) as there's no shell that interprets and expands /bin/??
这篇关于我不明白怎么execlp()在Linux中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!