About the directional output of the shell command 2>&1_dos/bat

Source: Internet
Author: User
Tags access properties stdin

MyCommand >mylog.txt 2>&1 should be the most classic usage.

The result of the command can be directed output in the form of "%>",% representing the file descriptor: 1 is standard output stdout, 2 is standard error stderr. The system default% value is 1, that is, "1>", and 1> can be abbreviated to, or the default is >. The default target for STDOUT is the terminal, and the default target for stderr is also the terminal. We execute in batches: Echo text >result.txt, we can see the echo text 1>result.txt on the screen, that's the truth.

Where & needs to be used directly with the redirection symbol.

Reference:
1,%261 "' >http://www.google.cn/search?q=" 2>%261 "
2, http://www.microsoft.com/technet/prodtechnol/windowsserver2003/zh-chs/library/ServerHelp/ 04969a04-a424-4776-bdc7-dc5066ce79b2.mspx?mfr=true

application Example:

1, output the results to Result.txt
net stop myservices >>result 2>&1

2, hidden program output results
net stop myservices >nul 2>nul

Microsoft's article on redirecting:

Using the command redirection operator

Renew Date: 01/21/2005

Using the command redirection operator

You can use redirection operators to redirect command input and output data streams from the default location to another location. The location of the input or output data stream is called a handle.

The following table lists the available handles.

Handle Digital code for handle Describe

Stdin

0

Keyboard input

STDOUT

1

Output to command Prompt window

STDERR

2

Error output to command Prompt window

UNDEFINED

3-9

Handles are defined by the application individually, and they are unique to each tool

The number 0 to 9 represents the first 10 handles. You can use the command Cmd.exe to run the program and redirect any handle in the first 10 handles of the program. To specify a handle to use, type the number of the handle before the redirection operator. If a handle is not defined, the default < redirect input operator is 0, and the default > Redirection output operator is 1. After you type the < or > operator, you must specify the read/write location of the data. You can specify a file name or other existing handle.

To specify that you want to redirect to an existing handle, use the and (&) character followed by the handle number (that is, the & handle number ) that you want to redirect. For example, the following command can redirect handle 2 (that is, STDERR) to handle 1 (that is, STDOUT):

2>&1

The following table lists the operators that can be used to redirect input and output data streams.

Redirection operator Describe

>

Writes the output of the command to a file or device, such as a printer, rather than a command prompt window or handle.

<

Reads the command input from a file instead of from a keyboard or handle.

>>

Add the command output to the end of the file without deleting the information already in the file.

>&

Writes the output of one handle to the input of another handle.

<&

Reads the input from a handle and writes it to another handle output.

|

Reads the output from one command and writes it to the input of another command. Also known as pipelines.

By default, you can send command input (that is, STDIN handle) from the keyboard to Cmd.exe, and then send the command output (that is, the STDOUT handle) to the command Prompt window by Cmd.exe.

redirect Input (<)

To redirect input to a file or device via the keyboard, use the < operator. For example, to get the input for the sort command from File.txt, type:

Sort<file.txt

The contents of the File.txt are displayed in the Command Prompt window in alphabetical order.

The < operator can open a specified filename with read-only access. Therefore, you cannot write information to a file when you use the operator. For example, if you start a program in <&2, all attempts to read handle 0 will fail because handle 2 is initially opened with write-only access.

Attention

0 is the default handle for the < redirect input operator.

REDIRECT Output (>)

Almost all commands send output to the command prompt window. Even commands that send output to a drive or printer display messages and prompts in a command prompt window.

To redirect output from a command prompt window to a file or device, use the > operator. You can use this operator in a number of commands. For example, to redirect the dir output to Dirlist.txt, type:

Dir>dirlist.txt

If Dirlist.txt does not exist, Cmd.exe creates the file. If Dirlist.txt exists, Cmd.exe replaces the information in the file with the output of the dir command.

To run the netsh routing dump command, and then send the output to Route.cfg, type:

netsh routing dump>c:\route.cfg

The > operator can open a specified file with write-only access. Therefore, you cannot use this operator to read files. For example, if you use the redirection operator >&0 to start a program, all attempts to write handle 1 will fail because handle 0 is initially opened as read-only access.

Attention

1 is the default handle for the > redirect Output operator.

Copy handle

The redirection operator & can copy output or input from one specified handle to another specified handle. For example, to send the dir output to File.txt and send the error output to File.txt, type:

Dir>c:\file.txt 2>&1

When you copy a handle, you can copy all the attributes of the original state of the handle. For example, if a handle has a read-only access property, all copies of the handle have read-only access properties. You cannot copy a handle with a read-only access property to another handle with a write-only access property.

redirect inputs and replicas using the & operator

To use the redirection input operator (<) in conjunction with the copy operator (&), the specified file must already exist. If the input file exists, Cmd.exe opens the file as read-only, and then sends the characters that the file contains as input to this command (as you would from keyboard input). If a handle is specified, Cmd.exe copies the specified handle to the existing handle of the system.

For example, to open File.txt in the form of a handle 0 input read (that is, STDIN), type:

< file.txt

To open File.txt and send the output to the Command Prompt window (that is, STDOUT) after the content is sorted, type:

sort< file.txt

To find File.txt, and then redirect handle 1 (that is, STDOUT) and handle 2 (that is, STDERR) to Search.txt, type:

FindFile File.txt>search.txt 2<&1

To copy user-defined handle 3 with handle 0 input read (that is, STDIN), type:

<&3

REDIRECT Output and replication using the & operator

If the output is redirected to a file and an existing file name is specified, Cmd.exe will open the file as write-only and overwrite the file's contents. If a handle is specified, Cmd.exe copies the file to an existing handle.

To copy user-defined handle 3 to handle 1, type:

>&3

To redirect all output including handle 2 (that is, STDERR) from the ipconfig command to handle 1 (that is, STDOUT), and then redirect the output to Output.log, type:

Ipconfig.exe>>output.log 2>&1

Additional output using the >> redirection operator

To add output from a command to the end of a file without losing any information that already exists in the file, use two consecutive greater-than numbers (that is, >>). For example, use the following command to attach the directory list generated by the dir command to the Dirlist.txt file:

Dir>>dirlist.txt

To attach the output of the netstat command to the end of Tcpinfo.txt, type:

Netstat>>tcpinfo.txt

Using the pipe operator (|)

The pipe operator (|) can extract the output of one command (STDOUT by default) and then direct it to the input of another command (STDIN by default). For example, you can categorize a directory by using the following command:

Dir | Sort

In this case, two commands are started at the same time, but then the sort command pauses until it receives the output of the dir command. The sort command uses the output of the dir command as input, and then sends the output to handle 1 (that is, STDOUT).

Merging commands with redirection operators

You can create custom commands by merging the filter commands with other commands and file names. For example, you can use the following command to store the file name containing the "LOG" string:

dir/b | Find "Log" Loglist.txt

The output of the dir command is sent through the Find filter command. The file name containing the string "LOG" is stored in the file Loglist.txt as a list of file names (for example, NetshConfig.log, LOGDAT.SVD, and Mylog.bat).

To use multiple filters in the same command, use the pipe (|) to separate the filters. For example, the following command searches each directory on disk C to find the filename containing the "LOG" string, and displays one screen at a time in the command Prompt window:

Dir c:\/s/b | Find "Log" | More

The

Pipe (|) enables you to orient the Cmd.exe to output through the Find filter command send dir command. The find command selects only the name of the file that contains the string "LOG". The more command displays the name of the file selected by the find command (one screen at a time in the Command Prompt window). For more information about filter commands, see Working with filters.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.