home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 45.2 The Story of  : #  #! Chapter 45
Shell Programming for the Initiated
Next: 45.4 Fun with #!
 

45.3 Don't Need a Shell for Your Script? Don't Use One

If your UNIX understands ( 44.4 ) files that start with:

#!

/interpreter/program

you don't have to use those lines to start a shell, such as #!/bin/sh . If your script is just starting a program like awk , UNIX can start the program directly and save execution time. This is especially useful on small or overloaded computers, or when your script has to be called over and over (such as in a loop).

First, here are two scripts. Both scripts print the second word from each line of text files. One uses a shell; the other runs awk directly:

% 

cat with_sh


#!/bin/sh
awk '
{ print $2 }
' $*
% 

cat no_sh


#!/usr/bin/awk -f
{ print $2 }
% 

cat afile


one two three four five

Let's run both commands and time ( 39.2 ) them:

% 

time with_sh afile


two
0.1u 0.2s 0:00 26%
% 

time no_sh afile


two
0.0u 0.1s 0:00 13%

One of the things that's really important to understand here is that when the kernel runs the program on the interpreter line, it is given the script's filename as an argument. If the intepreter program understands a file directly, like /bin/sh does, nothing special needs to be done. But a program like awk or sed requires the -f option if it is to read its script from a file. This leads to the seemingly odd syntax in the example above, with a call to awk -f with no following filename. The script itself is the input file!

One implication of this usage is that the interpreter program needs to understand # as a comment, or that first interpreter-selection line itself will be acted upon (and probably rejected by) the interpreter. (Fortunately, the shells, perl , sed , and awk do recognize this comment character.)

- JP


Previous: 45.2 The Story of  : #  #! UNIX Power Tools Next: 45.4 Fun with #!
45.2 The Story of : # #! Book Index 45.4 Fun with #!

The UNIX CD Bookshelf Navigation The UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System