(gawk) Executable Scripts
Info Catalog
(gawk) Long
(gawk) Running gawk
(gawk) Comments
Executable `awk' Programs
-------------------------
Once you have learned `awk', you may want to write self-contained
`awk' scripts, using the `#!' script mechanism. You can do this on
many Unix systems(1) (and someday on the GNU system).
For example, you could update the file `advice' to look like this:
#! /bin/awk -f
BEGIN { print "Don't Panic!" }
After making this file executable (with the `chmod' utility), you can
simply type `advice' at the shell, and the system will arrange to run
`awk'(2) as if you had typed `awk -f advice'.
$ advice
-| Don't Panic!
Self-contained `awk' scripts are useful when you want to write a
program which users can invoke without their having to know that the
program is written in `awk'.
*Caution:* You should not put more than one argument on the `#!'
line after the path to `awk'. This will not work. The operating system
treats the rest of the line as a single agument, and passes it to `awk'.
Doing this will lead to confusing behavior: most likely a usage
diagnostic of some sort from `awk'.
Some older systems do not support the `#!' mechanism. You can get a
similar effect using a regular shell script. It would look something
like this:
: The colon ensures execution by the standard shell.
awk 'PROGRAM' "$@"
Using this technique, it is _vital_ to enclose the PROGRAM in single
quotes to protect it from interpretation by the shell. If you omit the
quotes, only a shell wizard can predict the results.
The `"$@"' causes the shell to forward all the command line
arguments to the `awk' program, without interpretation. The first
line, which starts with a colon, is used so that this shell script will
work even if invoked by a user who uses the C shell. (Not all older
systems obey this convention, but many do.)
---------- Footnotes ----------
(1) The `#!' mechanism works on Linux systems, Unix systems derived
from Berkeley Unix, System V Release 4, and some System V Release 3
systems.
(2) The line beginning with `#!' lists the full file name of an
interpreter to be run, and an optional initial command line argument to
pass to that interpreter. The operating system then runs the
interpreter with the given argument and the full argument list of the
executed program. The first argument in the list is the full file name
of the `awk' program. The rest of the argument list will either be
options to `awk', or data files, or both.
Info Catalog
(gawk) Long
(gawk) Running gawk
(gawk) Comments
automatically generated byinfo2html