Go to the first, previous, next, last section, table of contents.

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(3) (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(4) 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.

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' "[email protected]"

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 "[email protected]" 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.)

Go to the first, previous, next, last section, table of contents.