DOWNLOAD THE CODE:
Download the Code Listing_01.txt

Perl provides a good shell scripting language

Sometimes you must be creative to make do with the tools you have. With a little bit of effort, you can mold, shape, and bend NT's command-line utilities in ways you might not have thought possible. This month, I'll demonstrate some of Perl's powerful text-processing capabilities and how they put you in control of NT's command line.

The ingredients I'll use include three Perl operators--backticks, grep, and map--and regular expressions. When you combine the Perl operators, you can slice and dice the output of any NT command-line utility into any format. You can format the output of one command so that another command can use it. Or you can reformat and combine the output of several commands to generate a report. First I'll explain how the three Perl operators work, and then I'll walk you through an example script.

Backticks
Enclosing a command in backtick, or backquote, characters (`) lets you execute the command from within a Perl script. The syntax is straightforward. For example, in the expression

@results = `net user`;

I've enclosed the command I want to invoke (NET USER) in backticks.

Backtick strings, like double-quoted strings, support variable interpolation. That is, when a script encounters a command enclosed in backticks, the script replaces the variable names with their current value before execution.

Following any variable substitution, the script passes the command to NT's command processor for execution; the script waits for the command to complete before continuing. At completion, the script captures the standard output of the command in string form. You can capture the command's output in a scalar variable or an array. My example statement (@results = `net user`;) executes NT's NET USER command and captures its standard output in an array named @results. Each element of @results corresponds to one line of NET USER's output. Figure 1 shows the @results values for my examples.

Perl provides quoted execution notation as another way to express the backtick command. This method lets you choose an alternative command delimiter for the backtick symbol. Each of the following statements is functionally equivalent to @results = `net user`;:

@results = qx/net user/;
@results = qx(net user);
@results = qx[net user];

Grep
Perl's grep function finds or counts matching elements in an array. For example, you can use grep to find all users whose first names begin with T; grep is similar to NT's findstr command. The syntax for grep is

grep(/expression/ or {code block}, @array);

Grep evaluates the expression or code block for each element in the array. If the expression is true, grep returns the corresponding element in the array. If you use grep in a scalar context, grep returns the number of times the expression was true.

For example, in the two statements

@newresults = grep(/sql/i, @results);   # array or list context
$numresults = grep(/sql/i, @results);   # scalar context

I use a regular expression (/sql/i) as the first argument and the @results array as the second argument. Grep successively evaluates the regular expression for each element of @results. Any line containing the string sql (i tells the function to ignore case) results in a match, or true expression.

For the contents of the @results array shown in Figure 1, Table 1 lists the values of the @newresults and $numresults variables. Notice that grep returns a value only when the expression is true. Also note that if your expression or code block modifies the contents of a list element, the corresponding element in the original list also changes.

   Prev. page   [1] 2     next page



You must log on before posting a comment.

If you don't have a username & password, please register now.

Reader Comments

This is excellent, exactly what I was looking for. Thanks, Windows 2000 Magazine has helped me in other situations too. Keep up the good work.

Jim Ambrose

Hi, Do you know if "$?" is working in perl for Windows. Because, i need to catch "error code". Thank You!

Nicolas Rineau