转载来源
http://www.xuebuyuan.com/1476323.html
所谓的Interpreter File
就是文件的首行为:
#! pathname [optional-argument]
的文件。
The recognition of these files is done within the kernel as part of processing the exec system call.
这个是由kernel,在exec
时认出该类文件并执行的。
exec
函数如下:
#include <unistd.h>
extern char **environ;
int execl(const char *pathname, const char *arg0, ...);
int execlp(const char *filename, const char *arg0, ...);
int execle(const char *pathname, const char * , ..., char * const envp[]);
int execv(const char *pathname, char *const argv[]);
int execvp(const char *filename, char *const argv[]);
int execve(const char *pathname, char *const argv[], char *const envp []);
注意 arg0 ,本来正常控制台下应该是程序名,在这里被丢掉了。
exec
会将const char *pathname
或const char filename
传入解释器,作为它的一个参数,。
这就可以解释为什么arg0不输出而const char *pathname
或const char *filename
输出。
来看一下这个东东运行的情况。
首先建立一个程序echoall
,用来输出所有的参数。
/*
* Filename: echoall.c
*/
#include <stdio.h>
int main(int argc, char* argv[])
{
int i;
for (i=0; i<argc; i++)
{
printf("argc[%d]: %s/n", i, argv[i]);
}
fflush(stdout);
}
然后写个Interpreter File bash.sh
,来调用echoall
#!/home/wyang/test/echoall test
这个文件中,就只有一行。就是用来调用echoall程序的。
而test
就是传给echoall
的参数。
好了,再写一个程序通过exec
来调用echoall
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
char *env_init[] = {"USER=unknown", "PATH=/tmp", NULL};
int main(void)
{
pid_t pid;
if ((pid = fork()) < 0)
{
perror("fork error");
return 0;
}
else if (pid == 0)
{
if (execlp("bash.sh", "echoall", "only 1 arg", (char*)0) < 0)
{
perror("execlp error");
return 0;
}
}
return 0;
}
这个程序就是用exec运行了bash.sh这个interpreter file。 然后我们看运行的结果。
argc[0]: /home/wyang/test/echoall
argc[1]: test
argc[2]: ./bash.sh
argc[3]: only 1 arg
说明
实际运行的是
echoall
这个程序,而不是bash.sh
本生。也就是执行的是Interpreter,而不是InterpreterFile。
参数
test
紧跟在arg[0]
后面。exec
传入的参数bash.sh
,echoall
中,只有第一个文件名参数bash.sh
传给了echoall
, 而第二个参数”echoall”,被丢弃了。exec
接下来的参数,only 1 arg
,也传给了echoall
。