Redirecting Standard Input, Output, and Error

 < Day Day Up > 



By default, commands take input from the parameters specified when they are called by the command shell and then send their output, including errors, to the standard console window. Sometimes, though, you’ll want to take input from another source or send output to a file or other output device such as a printer. You may also want to redirect errors to a file rather than the console window. You can perform these and other redirection tasks using the techniques introduced in Table 2-2 and discussed in the sections that follow.

Table 2-2: Redirection Techniques for Input, Output, and Errors

Redirection Technique

Description

command1 | command2

Sends the output of the first command to be the input of the second command.

command < [path]filename

Takes command input from the specified file path.

command > [path]filename

Sends output to the named file, creating the file if necessary or overwriting it if it already exists.

command >> [path]filename

Appends output to the named file if it exists or creates the file and then writes to it.

command < [path]filename > [path]filename

Gets command input from the specified file and then sends command output to the named file.

command < [path]filename >> [path]filename

Gets command input from the specified file and then appends command output to the named file.

command 2> [path]filename

Creates the named file and sends any error output to it. If the file exists, it is overwritten.

command 2>&1 filename

Sends error output to the same destination as standard output.

Redirecting Standard Output to Other Commands

Most commands generate output that can be redirected to another command as input. To do this, you use a technique called piping, whereby the output of a command is sent as the input of the next command. Following this, you can see the general syntax for piping is

Command1 | Command2

where the pipe redirects the output of Command1 to the input of Command2. But you can also redirect output more than once, such as

Command1 | Command2 | Command3

The two most common commands that are piped include FIND and MORE. The FIND command searches for strings in files or in text passed to the command as input and then lists the text of matching lines as output. For example, you could obtain a list of all .txt files in the current directory by typing the command

 dir | find ".txt " 

The MORE command accepts output from other commands as input and then breaks this output into sections which can be viewed one console page at a time. For example, you could page through a log file called Dailylog.txt using the following command:

type c:\working\logs\dailylog.txt | more

Type find /? or more /? at the command line to get a complete list of the syntax for these commands.

Redirecting I/O to and from Files

Another command redirection technique is to get input from a file using the input redirection symbol (<). For example, the following command sorts the contents of the Usernames.txt file and displays the results to the command line:

sort < usernames.txt

Just as you can read input from a file, you can also send output to a file. To do this, you can use > to create or overwrite, or >> to create or append data to a named file. For example, if you want to write the current network status to a file, you could use the command

netstat -a > netstatus.txt 

Unfortunately, if there is an existing file in the current directory with the same file name, this command overwrites the file and creates a new one. If you want to append this information to an existing file rather than overwrite an existing file, change the command text to read

netstat -a >> netstatus.txt

The input and output redirection techniques can be combined as well. You could, for example, obtain command-input from a file and then redirect command-output to another file. In this example, a list of user names is obtained from a file and sorted, and then the sorted name list is written to a new file:

sort < usernames.txt > usernames-alphasort.txt

Redirecting Standard Error

By default, errors from commands are written as output on the command line. If you are running unattended batch scripts or utilities, however, you may want to redirect standard error to a file so that errors are tracked. One way to redirect standard error is to tell the command line that errors should go to the same destination as standard output. To do this, type the 2>&1 redirection symbol as shown in this example:

chkdsk /r > diskerrors.txt 2>&1

Here, you send standard output and standard error to a file called Diskerrors.txt. If you want to see only errors, you can redirect only the standard error. In this example, standard output is displayed at the command line and standard error is sent to the file Diskerrors.txt:

chkdsk /r 2> diskerrors.txt



 < Day Day Up > 



Microsoft Windows Command-Line Administrator's Pocket Consultant
MicrosoftВ® WindowsВ® Command-Line Administrators Pocket Consultant
ISBN: 0735620385
EAN: 2147483647
Year: 2004
Pages: 114

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net