Interpreter file for Process Control

Source: Internet
Author: User

All current UNIX Systems Support interpreter files) (Also known as interpreter script ). This type of file is:

The space between the exclamation point and pathname is optional. The most common interpreter file starts with the following columns:

#! /Bin/sh

Pathname is usually an absolute path name.).. The process in which the kernel calls the exec function actually executes not the interpreter file, but the file specified by pathname in the first line of the interpreter file.. Make sure to include the interpreter File(Text File, which takes #! ) And interpreter(Specified by pathname in the first line of the interpreter file. (The interpreter file is a text file that specifies the interpreter used in the first line..)

Many systems have limits on the length of the first line of the interpreter file. These restrictions include #! , Pathname, optional parameters, line break termination, and number of spaces. This limit is 127 bytes in Linux.

Instance

Let's observe an instance and learn about it.When the executed file is an interpreter file, how does the kernel process the parameters of the exec function and the optional parameters of the first line of the interpreter file?. Program listing 8-10 calls exec to execute an interpreter file.

Program listing 8-10 execute a program for the interpreter File

[root@localhost apue]# cat prog8-<sys/wait.h>((pid = fork()) <  (pid == )       (execl(, , , ( *)) < (waitpid(pid, NULL, ) < )   

The interpreter file executed in program listing 8-10 is as follows (only one line is available ):

!/home/zhu/apue/echoarg foo

The program echoarg (Interpreter) returns each command line parameter (which is the program listing 7-3, see: http://www.cnblogs.com/nufangrensheng/p/3507949.html ).

Program list 8-10 running results:

[root@localhost apue]# ./prog8-]: /home/zhu/apue/]: /home/zhu/apue/]: MY ARG2

Note: When the kernel exec this interpreter (/home/zhu/apue/echoarg), argv [0] is the pathname of this interpreter, argv [1] is an optional parameter in the interpreter file, and other parameters are pathname (/home/zhu/apue/testinterp ), and the second and third parameters (myarg1 and MY ARG2) of execl are called in program listing 8-10 ). When you call execl, argv [1] and argv [2] have shifted the two locations (?) to the right (?). Note that the kernel takes the pathname in the execl call rather than the first parameter (testinterp), because in general, pathname contains more information than the first parameter (usually the first parameter is only part of pathname ).

In general, when the kernel exec interpreter, the command line parameters are the pathname of the interpreter, the optional parameters of the interpreter, the pathname of the interpreter file, and the parameters after argv [0] In the exec function call parameter list (not including argv [0], generally, argv [0] is only part of the interpreter file pathname.). (Mentioned in http://www.cnblogs.com/nufangrensheng/p/3510821.htmlWe can set argv [0] to any string.)

 

Instance

Optional parameters can be followed after the interpreter pathname. If an interpreter program supports the-f option, the optional parameter frequently used after pathname is-f.

For example, you can run the awk (1) program in the following ways:

awk -f myfile

It tells awk to read the awk program from the file myfile.

If you use the-f option in the interpreter file, you can write it as follows:

#!/bin/awk -

For example, the 8-11 Program is an interpreter file such as/usr/local/bin/awkexample.

Program listing 8-11 as an awk program for the interpreter File

#!/bin/awk -(i=; i<ARGC; i++

If one of the path prefixes is/usr/local/bin, you can run the program listing 8-11 in the following ways:

] =] =] =] = f3

In fact, shell will call exec as follows:

exec(, , , , NULL);

However, exec actually executes the interpreter/bin/awk instead of the interpreter file awkexample, and its command line parameter is:

/bin/awk -f /usr/local/bin/awkexample file1 FILENAME2 f3

 

Do I need an interpreter file? That's not exactly the case. However, they do benefit users in efficiency.The cost is the extra kernel overhead.(Because the interpreter file is identified by the kernel ).

Interpreter files are useful for the following reasons:

(1) Some programs are scripts written in some language. The Interpreter file can hide this fact. For example, to execute the program 8-11, you only need to use the following command line:

awkexample opitonal-arguments

You do not need to know that the program is actually an awk script. Otherwise, you need to execute the program in the following ways:

awk -f awkexample optional-arguments

(2) The Interpreter script also provides benefits in terms of efficiency. Generally, replacing the interpreter script with a shell script requires more overhead.

(3) The Interpreter script allows us to write shell scripts using other shells except/bin/sh. (In this case, you only need to specify the shell used in the first line of the interpreter script.

This blog is excerpted from advanced programming for UNIX environments (version 2) and used only for personal learning records. For more information about this book, see:Http://www.apuebook.com/.

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.