Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 8.12 Interpreter Files 263


When/bin/awkis executed, its command-line arguments are
/bin/awk -f /usr/local/bin/awkexample file1 FILENAME2 f3
The pathname of the interpreter file (/usr/local/bin/awkexample) is passed to the
interpreter.The filename portion of this pathname (what we typed to the shell) isn’t
adequate, because the interpreter (/bin/awkin this example) can’t be expected to use
thePATHvariable to locate files. When it reads the interpreter file,awkignores the first
line, since the pound sign isawk’s comment character.
We can verify these command-line arguments with the following commands:
$/bin/su become superuser
Password: enter superuser password
#mv /usr/bin/awk /usr/bin/awk.save save the original program
#cp /home/sar/bin/echoarg /usr/bin/awk and replace it temporarily
#suspend suspend the superuser shell
[1] + Stopped /bin/su using job control
$awkexample file1 FILENAME2 f3
argv[0]: /bin/awk
argv[1]: -f
argv[2]: /usr/local/bin/awkexample
argv[3]: file1
argv[4]: FILENAME2
argv[5]: f3
$fg resume superuser shell using job control
/bin/su
#mv /usr/bin/awk.save /usr/bin/awk restorethe original program
#exit and exit the superuser shell
In this example, the-foption for the interpreter is required. As we said, this tellsawk
where to look for theawkprogram. If we remove the-foption from the interpreter
file, an error message usually results when we try to run it. The exact text of the
message varies, depending on wherethe interpreter file is stored and whether the
remaining arguments represent existing files. This is because the command-line
arguments in this case are
/bin/awk /usr/local/bin/awkexample file1 FILENAME2 f3
andawkis trying to interpret the string/usr/local/bin/awkexampleas anawk
program. If we couldn’t pass at least a single optional argument to the interpreter (-f
in this case), these interpreter files would be usable only with the shells.

Areinterpreter files required? Notreally.They provide an efficiency gain for the
user at some expense in the kernel (since it’s the kernel that recognizes these files).
Interpreter files areuseful for the following reasons.


  1. They hide that certain programs arescripts in some other language. For
    example, to execute the program in Figure8.21, we just say
    awkexampleoptional-arguments

Free download pdf