Targeted output of shell commands 2gt; 1

Source: Internet
Author: User

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

Command results can be output in the form of "%>", % indicates the file descriptor: 1 indicates the standard output stdout, and 2 indicates the standard error stderr. The default value of % is 1, that is, "1>", and 1> can be abbreviated as>, that is, the default value is>. The default destination of stdout is terminal, and the default destination of stderr is terminal. Execute echo text> result.txt in batch processing, and we can see echo text 1> result.txt on the screen.

In this example, You Need To directly use the redirection symbol.

Refer:
1, 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 instance:

1st worker outputs the result to result.txt
Net stop myservices> result 2> & 1

2. Hide program output results
Net stop myservices> nul 2> nul

Microsoft's article on redirection:

Use the command redirection operator to update the date: 01/21/2005

Use the command redirection Operator

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

The following table lists available handles.

Handle Numeric code of the handle Description

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 separately by applications. They are unique to each tool.

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

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

2> & 1

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

Redirection Operator Description

>

Write command output to a file or device (such as a printer) instead of a command prompt window or handle.

<

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

>

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

> &

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

<&

Read the input from one handle and write it into another handle output.

|

Read the output from one command and write it into the input of another command. It is also called an MPS queue.

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

Redirect input (<)

To redirect input to a file or device through a keyboard, use the <operator. For exampleSortEnter the following command:

Sort <file.txt

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

<Operator can open a specified file name with read-only access permission. Therefore, you cannot write information to the file when using this operator. For example, if you start the program with <& 2, all operations that attempt to read handle 0 will fail, because handle 2 is initially opened in write-only access mode.

Note:

0 is the default handle of the redirection input operator.

Redirect output (>)

Almost all commands send the output to the Command Prompt window. Messages and prompts are displayed in the Command Prompt window even if the command output is sent to the drive or printer.

To redirect the output from the command prompt window to a file or device, use the> operator. This operator can be used in many commands. For exampleDirThe output is redirected to Dirlist.txt. Type:

Dir> dirlist.txt

If Dirlist.txt does not exist, cmd.exe creates the file. If Dirlist.txt exists, cmd.exe usesDirCommand output replaces the information in the file.

To runNetsh routing dumpCommand, 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 permission. Therefore, you cannot use this operator to read files. For example, if you use the redirection operator> & 0 to start the program, all attempts to write handle 1 will fail, because handle 0 was initially opened in read-only access mode.

Note:

1 Yes> default handle of the redirection output operator.

Copy handle

The redirection operator can copy the output or input from one specified handle to another. For exampleDirThe output is sent to File.txt and the error output is sent to File.txt. Type:

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

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

Use & operator to redirect input and copy

To use the redirected input operator (<) with the copy operator (&), the specified file must already exist. If the input file exists, cmd.exe will open the file in read-only mode, and then send the characters contained in the file as input to this command (as input from the keyboard ). If a period 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 reading (STDIN) with the handle 0 input, type:

<File.txt

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

Sort <File.txt

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

Findfile file.txt> search.txt 2 <& 1

To copy the User-Defined handle 3 by reading (STDIN) with the handle 0 input, type:

<& 3

Use & operator to redirect output and copy

If the output is redirected to a file and the current file name is specified, cmd.exe will open the file in write-only mode and overwrite the file content. If you specify a period, cmd.exe copies the file to the existing handle.

To copy custom handle 3 to handle 1, type:

> & 3

To extract all the output including handle 2 (that is, STDERR) fromIpconfigCommand redirection to handle 1 (that is, STDOUT), and then redirect the Output to Output. log. Type:

Ipconfig.exe> output. log 2> & 1

Use> redirect operator to append the output

To add the output to the end of the file without losing any existing information in the file, use two consecutive greater than signs (> ). For example, use the following commandDirThe directory list generated by the command is appended to the Dirlist.txt file:

Dir> dirlist.txt

ToNetstatThe command output is appended to the end of Tcpinfo.txt. Type:

Netstat> tcpinfo.txt

Use the pipeline operator (|)

The pipeline operator (|) can extract the output of a command (STDOUT by default) and direct it to the input of another command (STDIN by default. For example, you can use the following command to classify directories:

Dir | sort

In this example, two commands are started at the same timeSortThe command is paused until it receivesDirCommand output.SortCommand usageDirCommand output is used as the input, and then the output is sent to handle 1 (that is, STDOUT ).

Merge commands with redirection Operators

You can create custom commands by combining 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

DirCommand output is throughFindFilter command. The file name that contains the string "LOG" is stored in the Loglist.txt file as the file name list (for example, NetshConfig. log, Logdat. svd, and Mylog. bat.

To use multiple filters in the same command, use pipelines (|) to separate filters. For example, the following command searches for each directory on the C drive to find the file name containing the "LOG" string, and displays a screen at each time in the Command Prompt window:

Dir c: \/s/B | find "log" | more

Pipeline (|) can be used to redirect Cmd.exe to allow it to pass throughFindFilter command sendingDirCommand output.FindCommand to select only the file name that contains the string "LOG.MoreCommand to displayFindName of the file selected by the Command (a screen is displayed each time in the Command Prompt window ). For more information about filter commands, see use 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.