📄 ch16.htm
字号:
still specify that information is redirected. But in this case,
instead of replacing what was already there, the new information
is added to it. This process is more like "Water >>
Bucket," where you're adding more water instead of emptying
the bucket. Getting much water moved would be really hard if you
emptied the bucket every time. So, if you take the container <TT><FONT FACE="Courier">myfile.txt</FONT></TT>,
which you created before, and use the line <TT><FONT FACE="Courier">echo
Hi, how are ya? >> myfile.txt</FONT></TT>, you just increase
the size of <TT><FONT FACE="Courier">myfile.txt</FONT></TT> because
you're adding the text to the file that already exists instead
of replacing it.
</BLOCKQUOTE>
<H5>IF</H5>
<BLOCKQUOTE>
Sometimes you want the program to perform different tasks, depending
on a certain condition or comparison. The basic method of comparison
is the <TT><FONT FACE="Courier">IF</FONT></TT> statement. You
can include several options in an <TT><FONT FACE="Courier">IF</FONT></TT>
statement, but two of the most useful tasks are string comparison
(for checking environment variables) and file existence (for making
sure that you don't try to send back a data file that doesn't
exist). First, look at a line that compares two strings:
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">IF %REQUEST_METHOD%==POST goto DOSTUFF</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
Here, if the environment variable <TT><FONT FACE="Courier">REQUEST_METHOD</FONT></TT>
is <TT><FONT FACE="Courier">POST</FONT></TT>, the program jumps
to the section labeled <TT><FONT FACE="Courier">DOSTUFF</FONT></TT>.
Everything after the <TT><FONT FACE="Courier">IF <I>string1</I>==<I>string2</I></FONT></TT>
format is space for a command to take place; you can use <TT><FONT FACE="Courier">copy
a:\data.txt</FONT></TT> or <TT><FONT FACE="Courier">delete c:\temp\filelock.txt</FONT></TT>,
for example, or any other DOS-level command that you want.
</BLOCKQUOTE>
<H5>FOR</H5>
<BLOCKQUOTE>
The <TT><FONT FACE="Courier">FOR</FONT></TT> command enables you
to perform the same operation on many different things. It uses
a replaceable variable, in which each element that it runs through
puts data in the same variable and does a specific function to
that variable every time the contents change. This command comes
in handy. If you have 30 text files, for example, and you want
to make them all into one big file called <TT><FONT FACE="Courier">bigfile.txt</FONT></TT>,
you can use the following line:
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">FOR %%f IN(c:\stuff\*.txt) DO type %%f
>> c:\bigfile.txt</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
Later in this chapter, as you see different examples such as storing
forms data in "BASIC and Its Cousins," you'll learn
how the <TT><FONT FACE="Courier">FOR</FONT></TT> command can be
useful.
</BLOCKQUOTE>
<H5>DOS-Level Commands</H5>
<BLOCKQUOTE>
Because you're using DOS already, you might as well make use of
built-in functions, like <TT><FONT FACE="Courier">DIR</FONT></TT>,
<TT><FONT FACE="Courier">COPY</FONT></TT>, <TT><FONT FACE="Courier">DELETE</FONT></TT>,
and others that give you some useful functions without a lot of
work. Say that you want to look through a file to find specific
instances of a word and return them to a user. Most programs can
do this task, though in some cases it would be laborious. UNIX
users search this way all the time by using the <TT><FONT FACE="Courier">grep</FONT></TT>
command. Often, most people don't use DOS's <TT><FONT FACE="Courier">FIND</FONT></TT>
command to do the same thing. You see an example of this use shortly,
but I wanted to add this important reminder: Don't despair that
DOS can't do something until you've checked all your options.
</BLOCKQUOTE>
<H4>Example: Viewing the Content File Data</H4>
<P>
Knowing what your program is receiving so that you can plan accordingly
is vital to the success of any CGI program. You've seen the basics
of a <TT><FONT FACE="Courier">CONTENT_FILE</FONT></TT> dump before,
but now you're going to see how easy it is to make the utility
that performs the dump-just three lines. First, you need to set
up a Content-type header to let the server and the browser know
what kind of information you're sending. Next, you need to print
a blank line, to separate the Content-type header from the rest
of the content. Finally, you need to print out the whole Content
File. Here's how you view the Content File as plain text output:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">echo Content-type: text/plain > %OUTPUT_FILE%
<BR>
</FONT></TT>echo. >> %OUTPUT_FILE%<BR>
<TT><FONT FACE="Courier">type %CONTENT_FILE% >> %OUTPUT_FILE%</FONT></TT>
</BLOCKQUOTE>
<H4>Example: Storing the Content File and Responding</H4>
<P>
Now say that you want to store the Content File information in
a big file so that you can go through it later, at your leisure.
You also want to send something back to the users to let them
know that you've received their information. This process is a
simple variation on viewing the file-it's all in how you redirect
output.
<P>
As I mentioned at the beginning of this chapter, this process
takes just two lines and the HTML information you want to send
back. First, you need to add the Content File to an existing file.
Then you need to send back a file that you've made that contains
any information or message you want to send. We'll look at them
in pieces, starting with how you store content file data and send
a fixed response:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">type %CONTENT_FILE% >> myfile.txt
<BR>
type message.txt > %OUTPUT_FILE%</FONT></TT>
</BLOCKQUOTE>
<P>
These lines of code produce the following output, assuming that
the file <TT><FONT FACE="Courier">message.txt</FONT></TT> contains
"Hi, how are ya?":
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Content-type: text/html<BR>
<BR>
Hi, how are ya?</FONT></TT>
</BLOCKQUOTE>
<P>
As you can see, there's nothing to this process! Sure, this code
is not the prettiest or most useful as it exists, but it checks
that CGI is indeed running on your machine and gives you a base
to build on for later.
<H4>Example: DOS Text Search</H4>
<P>
This example is much more useful than just echoing back information
that the user already knows about. This example enables you to
take the <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT> environment
variable (one word only), search through a text file, and then
display the matches. And it takes no effort whatsoever on your
part because you use the built-in DOS <TT><FONT FACE="Courier">find</FONT></TT>
command. All you need is the text file and about two minutes to
type in the BAT program shown in Listing 16.2. You can use it
for any kind of utility where you need a keyword search, such
as a listing of projects, classes, or directions.
<HR>
<BLOCKQUOTE>
<B>Listing 16.2. A DOS text search.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"># Basic DOS text searching<BR>
# -Uses one-word QUERY_STRING to search<BR>
# Bill Schongar, July 1996<BR>
<BR>
# First, we need the word we're looking for<BR>
set word=%QUERY_STRING%<BR>
<BR>
# Now we search<BR>
find "%word%" c:\myfile.txt > c:\results.txt<BR>
<BR>
# Send output<BR>
echo Content-type: text/html > %OUTPUT_FILE%<BR>
echo. >> %OUTPUT_FILE%<BR>
type c:\results.txt >> %OUTPUT_FILE%</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
But what if you have a variety of files you want to search through
for the word? Then you use the <TT><FONT FACE="Courier">FOR</FONT></TT>
statement in the batch file and go through all the files, as shown
in Listing 16.3.
<HR>
<BLOCKQUOTE>
<B>Listing 16.3. A BAT file to search multiple text files.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"># DOS CGI - Search multiple text files
for keyword<BR>
# -Uses one-word QUERY_STRING to search<BR>
# Bill Schongar, July 1996<BR>
<BR>
# First, we need the word we're looking for<BR>
set word=%QUERY_STRING%<BR>
<BR>
# Now we search<BR>
for %%f IN (c:\files\*.txt) DO find "%word%"
%%f > c:\results.txt<BR>
<BR>
# Send output<BR>
echo Content-type: text/html > %OUTPUT_FILE%<BR>
echo. >> %OUTPUT_FILE%<BR>
type c:\results.txt >> %OUTPUT_FILE%</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
See? No problem. Even the most simplistic of languages, when combined
with a few useful commands, can get the job done without much
pain or effort.
<H3><A NAME="BASICandItsCousins">BASIC and Its Cousins</A></H3>
<P>
Built into most DOS systems is either BASIC, QBasic, or some optional
form thereof. These languages provide more advanced operations
than can be had with regular BAT files, because you have more
operations that you can perform. You can take advantage of being
able to extract portions of strings, use variables more effectively,
gain better control over file input and output-all the niceties
that make what you can do more powerful.
<P>
The problem with BASIC isn't necessarily doing what you want to-though
how easily you do it is another matter-but rather how you can
use what you created after you're done creating it. Many BASIC
implementations don't really have a concept of a runtime version
of your program. They run from the editor, or they run from the
command line and then spawn the editor, but they most often don't
provide an easy way to say "Run this, but don't bring up
the editor when you're done!" QBasic, one of the more common
implementations of BASIC because it's built into MS-DOS, is guilty
of this heinous problem. Its <TT><FONT FACE="Courier">/run</FONT></TT>
flag just says, "Oh, run first, editor second," not
"Just run the program."
<P>
To get around this problem, you may be forced to compile the application
into an EXE or COM file. If you have a variety of programming
tools at your disposal, even old ones, you can normally find what
you need hiding in and among them. The old Visual Basic for DOS,
for instance, had a compiler that would do the job.
<H4>Example: Storing Form Data</H4>
<P>
To give you an idea of some of the more useful jobs you can perform
with a more advanced programming language, see Listing 16.4 for
an example done in Qbasic.
<HR>
<BLOCKQUOTE>
<B>Listing 16.4. Storing form data and returning information using
QBasic.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">REM DOS CGI Qbasic Form Processor<BR>
REM -Stores Firstname, Lastname and email<BR>
<BR>
REM First, we need to get the filenames<BR>
infile$ = ENVIRON$("CONTENT_FILE")<BR>
REM infile$ = "c:\temp\mine.txt"<BR>
REM outfile$ = "c:\temp\yours.txt"<BR>
outfile$ = ENVIRON$("OUTPUT_FILE")<BR>
savefile$ = "c:\temp\save.txt"<BR>
<BR>
REM Now we'll open up the content file<BR>
REM and read the line it contains<BR>
OPEN infile$ FOR INPUT AS #1<BR>
LINE INPUT #1, b$<BR>
<BR>
REM Find out how long the string is<BR>
a = LEN(b$)<BR>
<BR>
REM Now use a laborious method to check each character<BR>
REM and determine where to break into expected pairs<BR>
FOR i% = 1 TO a<BR>
c$ = MID$(b$,
i%, 1)<BR>
IF c$ = "&"
THEN<BR>
m%
= m% + 1<BR>
marker(m%)
= i%<BR>
END IF<BR>
NEXT i%<BR>
<BR>
REM We're expecting three pairs, which means there<BR>
REM should be two markers. We'll use each marker<BR>
REM as a method of extracting the information before,<BR>
REM after, or in between them. That's what MID$ does.<BR>
REM There are going to be equal (=) signs in there,<BR>
REM but for the moment it doesn't matter. They can<BR>
REM be parsed out just like the & signs.<BR>
firstname$ = MID$(b$, 1, (marker(1) - 1))<BR>
lastname$ = MID$(b$, (marker(1) + 1), ((marker(2) - 1) - (marker(1))))
<BR>
email$ = MID$(b$, (marker(2) + 1), (a - (marker(2))))<BR>
CLOSE #1<BR>
<BR>
REM Now let's send some output back, customized<BR>
OPEN outfile$ FOR OUTPUT AS #2<BR>
PRINT #2, "Content-type: text/html"<BR>
PRINT #2, "<BR>
PRINT #2, "<title> Received</title>"<BR>
PRINT #2, "Thanks for filling out the form,"<BR>
PRINT #2, "<br> We'll email you something soon."
<BR>
CLOSE #2<BR>
<BR>
REM And we want to keep track of our visitors, so<BR>
REM we'll add them to a file<BR>
OPEN savefile$ FOR appeND AS #3<BR>
PRINT #3, "--------------------"<BR>
PRINT #3, "Visitor Data"<BR>
PRINT #3, "; firstname$<BR>
PRINT #3, "; lastname$<BR>
PRINT #3, "; email$<BR>
CLOSE #3</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
The program starts out by setting up some variables for referencing
the Input File and the Output File, so that you don't have to
keep typing the environment variable strings in over and over.
As you can see from the comments (the lines beginning with REM),
it then goes step by step through the data, running through a
number of contortions to get the incoming data to be what we expect
it to.
<H3><A NAME="PerlforDOS">Perl for DOS</A></H3>
<P>
For some time, Perl was available only to folks in UNIX land.
Then it slowly started gaining momentum and moving to other platforms,
and the last platform to get it was good old 16-bit DOS. When
you want to move beyond what you can easily do in BAT and CMD
files, you can move up to Perl. The good thing about this migration
is that Perl is enormously popular for creating CGI scripts, and
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -