Linux Interpreter principle __linux

Source: Internet
Author: User

The person who will shell programming certainly will not be unfamiliar to #!/bin/sh. What does this line of string mean in the end?

It should be clear that the so-called interpreter refers to the executable program following the #! line.

We start with the EXEC function.

The EXEC function includes a total of six functions.

#include <unistd.h>

int execl (const char *pathname,const char *arg0,.../* (char *) 0 */);

int execv (const char *pathname,char *const argv[]);

int execle (const char *pathname,const char *arg0,.../* (char *) 0,char *const envp[] * *);

int Execve (const char *pathname,char *const Argv[],char *const envp[]);

int EXECLP (const char *filename,const char *arg0,.../* (char *) 0 */);

int EXECVP (const char *filename,char *const argv[]);

These functions work the same way: execute a new piece of code.

Where the EXECL function, the first parameter path is the path of the executable that set the execution bit, the variable argument list that follows points to the list of arguments passed to this execution file (including parameter 0, which is the name of the executing file), and the last parameter (char *) 0 indicates the end of the argument list.

For the interpreter, the EXEC function (in the case of EXECL) executes so that if path is pointing to a script and the first line of the script starts with a #!, then this is called:

The execution of a new program is formed by using the string following the #! as a command followed by a parameter specified by the EXECL argument list.

The following is an example of an interpreter file in UNIX advanced environment programming.

First we create an interpreter text under/home/qiqijianglu:

The name of the text is Testinterp

The contents of the text are as follows:

#!/home/qiqijianglu/echoarg Foo

----------------------------------------------------------------------------

Divider line and divider line below is not the content in the text, the Echoarg program content is as follows:

#include "apue.h"

int main (int argc,char *argv[])

{

int i;

for (i=0;i<argc;i++)

printf ("argv[%d]:%s\n", I,argv[i]);

Exit (0);

}

-------------------------------------------------------------------------------

So Echoarg is an interpreter that echoes each command-line argument.

We'll write a program that executes an interpreter file named 197.C with the following code:

#include "apue.h"

#include <sys/wait.h>

int main (void)

{

pid_t pid;

if ((Pid=fork ()) <0)

Err_sys ("fork Error");

else if (pid==0)

{

if (Execl ("/home/qiqijianglu/testinterp", "Testinterp", "Myarg1", "My ARG2", (char *) 0) <0)

Err_sys ("Execl error");

}

if (Waitpid (pid,null,0) <0)

Err_sys ("Waitpid error");

Exit (0);

}

---------------------------------------------------------------------------------------------------

So we use the command cc 197.C to compile 197.c, and then run the program./a.out

Argv[0]:/home/qiqijianglu/echoarg
ARGV[1]: foo
ARGV[2]:/home/qiqijianglu/testinterp
ARGV[3]: Myargl
ARGV[4]: My ARG2

Execl is the way to execute a command:

The/home/qiqijianglu/testinterp content is the string that follows #!/home/qiqijianglu/echoarg Foo then #!/home/qiqijianglu/echoarg Foo plus the command line argument list makes up the new program line/home/qiqijianglu/echoarg Foo/home/qiqijianglu/testinterp Myargl my ARG2

Note that the kernel takes the pathname in the EXECL call instead of the first argument Testinterp because, in general, pathname contains more information than the first parameter.

For the Testinterp script, when we call it in the shell, the shell calls Fork,exec,wait to execute it, and first, the EXEC function interprets the #! row to the interpreter of the script/home/qiqijianglu/echoarg , and then the command line is processed into/home/qiqijianglu/echoarg foo./testinterp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.