Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


May 1998

Commanding NT's Command Line


RSS
Subscribe to Windows IT Pro | See More Task Automation Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

Download the Code Here

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.

   Previous  [1]  2  Next 


Top Viewed ArticlesView all articles
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Where is Microsoft NetMeeting in Windows XP?

...


Task Automation Whitepapers Essential Guide to E-discovery and Recovery for Microsoft Exchange

Continuous Data Protection and Recovery for Microsoft Exchange

Protecting (You and) Your Data with Exchange Server 2007

Related Events Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Cloud Computing Forum: Integrating Software, Server and Storage as a Service into Your Enterprise IT Delivery Model

Virtualization Forum: Optimizing Storage, Networks, Desktops, and Security

Check out our list of Free Email Newsletters!

Task Automation eBooks Spam Fighting and Email Security for the 21st Century

A Guide to Windows Certification and Public Keys

Keeping Your Business Safe from Attack: Patch Management

Related Task Automation Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing