Listing 1
Listing 2
In-Line Input or Bust
Leor Zolman
All standard UNIX shells support a nifty method for
embedding standard
input data within the very shell script that reads that
data. The
syntax for using such in-line input is as follows:
command-name parameters <<-END
input line 1
input line 2
.
.
.
input line n
END
With this form of the construct, all text between the
END markers (any text pattern may be used in place of
END)
is fed to the standard input of the command-name command.
The
hyphen before the first END says to strip all whitespace
from the
beginning of each line of the input data; this allows
the source text
to be formatted with arbitrary indentation for clarity,
even when
such indentation might confuse the command that will
actually read
the input.
To see the technique in action, submit the following
command to your
shell:
wc <<-STOP
this is
a
test
STOP
The system should respond with:
3 4 15
So far, this is all basic shell stuff. Any command that
accepts data on its standard input stream can be supplied
with in-line
data using this technique. However, what do you do with
a program
that does not accept any input from the standard input
stream,
but only knows how to take its input from a physical
file named on
the program's command line? The only way to feed literal
input to
such a program is to manually create a temporary file
containing the
required text, run the program with the name of that
file as a parameter,
and then remove the temporary file after the program
has finished
running.<>
|