unix解释器文件

转载来源

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 *pathnameconst char filename传入解释器,作为它的一个参数,。
这就可以解释为什么arg0不输出而const char *pathnameconst 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

说明

  1. 实际运行的是echoall这个程序,而不是bash.sh本生。

    也就是执行的是Interpreter,而不是InterpreterFile。

  2. 参数test紧跟在arg[0]后面。

  3. exec传入的参数bash.sh, echoall中,只有第一个文件名参数bash.sh传给了echoall, 而第二个参数”echoall”,被丢弃了。

  4. exec接下来的参数,only 1 arg,也传给了echoall

猜你喜欢

转载自blog.csdn.net/guiqulaxi920/article/details/78974366