manpagez: man pages & more
info gawk
Home | html | info | man

gawk: Executable Scripts

 
 1.1.4 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
 systems.(1)  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), simply
 type 'advice' at the shell and the system arranges to run 'awk' as if
 you had typed 'awk -f advice':
 
      $ chmod +x advice
      $ advice
      -| Don't Panic!
 
 (We assume you have the current directory in your shell's search path
 variable [typically '$PATH'].  If not, you may need to type './advice'
 at the shell.)
 
    Self-contained 'awk' scripts are useful when you want to write a
 program that users can invoke without their having to know that the
 program is written in 'awk'.
 
                           Understanding '#!'
 
    'awk' is an "interpreted" language.  This means that the 'awk'
 utility reads your program and then processes your data according to the
 instructions in your program.  (This is different from a "compiled"
 language such as C, where your program is first compiled into machine
 code that is executed directly by your system's processor.)  The 'awk'
 utility is thus termed an "interpreter".  Many modern languages are
 interpreted.
 
    The line beginning with '#!' lists the full file name of an
 interpreter to run and a single 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 contains either
 options to 'awk', or data files, or both.  (Note that on many systems
 'awk' may be found in '/usr/bin' instead of in '/bin'.)
 
    Some systems limit the length of the interpreter name to 32
 characters.  Often, this can be dealt with by using a symbolic link.
 
    You should not put more than one argument on the '#!' line after the
 path to 'awk'.  It does not work.  The operating system treats the rest
 of the line as a single argument and passes it to 'awk'.  Doing this
 leads to confusing behavior--most likely a usage diagnostic of some sort
 from 'awk'.
 
    Finally, the value of 'ARGV[0]' (⇒Built-in Variables) varies
 depending upon your operating system.  Some systems put 'awk' there,
 some put the full pathname of 'awk' (such as '/bin/awk'), and some put
 the name of your script ('advice').  (d.c.)  Don't rely on the value of
 'ARGV[0]' to provide your script name.
 
    ---------- Footnotes ----------
 
    (1) The '#!' mechanism works on GNU/Linux systems, BSD-based systems,
 and commercial Unix systems.
 
© manpagez.com 2000-2018
Individual documents may contain additional copyright information.