Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

May 01, 1998 12:00 AM

Commanding NT's Command Line

Windows IT Pro
InstantDoc ID #3071
Rating: (0)
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.

Related Content:

ARTICLE TOOLS

Comments
  • Nicolas Rineau
    9 years ago
    Feb 03, 2003

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

  • Jim Ambrose
    12 years ago
    Jun 26, 2000

    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.

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.