Reader to Reader - 25 Oct 1999

This month, one scripting solution is provided: Use Command-Line Operators to Their Fullest Extent

Readers

October 25, 1999

3 Min Read
ITPro Today logo in a gray background | ITPro Today

[Editor's Note: Email your scripting solutions (400 words or less) to Reader to Reader at [email protected]. Please include your script and phone number. We'll edit submissions for style, grammar, and length. If we print your contribution, you receive $100.]

People commonly use the following redirection symbols:

>   For redirecting the output of a command to a file
<   For redirecting the input of a command from a file
|   For piping the output of a command to the input of another command
>>  For appending the output of a command to a file

However, to get full use of these redirection symbols, you need to understand the difference between the three standard file handles in command-line programs: standard input (STDIN), standard output (STDOUT), and standard error (STDERR). File handles are numbers that a program uses to identify open files or pipes. STDIN is 0, STDOUT is 1, and STDERR is 2.

When you use unmodified redirection symbols, you're using STDIN and STDOUT; STDERR remains attached to the console. For example, if you run the Dir command

dir foo >stdout.txt

where foo is a nonexistent file, the message File Not Found appears, even though the system redirects all other files to stdout.txt.

To redirect STDERR to a file, place STDERR's value (i.e., 2) before the > symbol. For example, the command

dir foo >stdout.txt 2>stderr.txt

captures all the Dir command's output and redirects it to the respective files.

Adding the value of 2 doesn't work for piping. However, you can attach STDERR to STDOUT, then redirect them together. For example, the command

dir foo >out.txt 2>&1

attaches STDERR to STDOUT, then redirects STDOUT (and STDERR) to the file out.txt. Thus, to pipe both STDERR and STDOUT, you can use the command

dir foo 2>&1 | sort

to attach STDERR to STDOUT, pipe STDOUT (and STDERR), and sort the results. (For more information about file-handle manipulation, see "Chapter 4, OS/2 Command Symbols" of the OS/2 1.3 manual.)

When redirecting files or performing other tasks with command-line programs, you can use conditional processing symbols to control a command's execution. For example, you can use parentheses to group commands within a command-line statement:

(dir foo & dir bar) 2>&1 | sort

In this code, the parentheses combine the output from the two Dir commands and pipe it to the Sort command. Windows NT Help documentation outlines the conditional processing symbols. Under the Start menu, select Help. Go to commands, Windows NT, and click Display. Select Commands Index, and click Display. Click conditional processing symbols.

Two other command-line tricks are to use the nul file as a discard file and to force the Echo command to output a blank line. To discard all output from a command, use the nul file in a command such as

dir >nul 2>nul

or

dir >nul 2>&1

To force the Echo command to output a blank line, type

echo. >foo.txt

The period after echo tricks the Echo command into thinking data exists (which prevents the command from returning ECHO is on). However, because the Echo command operates on everything following the first character after the command, it outputs only a blank line.

—Toby Everett
[email protected]

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like