In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article is about the sample analysis of directed output in the shell command. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Mycommand > mylog.txt 2 > & 1 should be the most classic usage.
The result of the command can be directed in the form of "% >", with% indicating the file descriptor: 1 is the standard output stdout, 2 is the standard error stderr. The default% value of the system is 1, which is "1 >", while 1 > can be abbreviated to >, that is, the default is >. The default target for stdout is the terminal, and the default target for stderr is also the terminal. We execute it in a batch: echo text > result.txt, and we can see echo text 1 > result.txt on the screen. That's why.
Where & needs to be used directly in conjunction 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 result to result.txt
Net stop myservices > > result 2 > & 1
2. Hide the output of the program
Net stop myservices > nul 2 > nul
Microsoft's article on redirection:
Last Update at: 01Compact 21Compact 2005
Use the command to redirect the operator
You can use the redirect operator 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.
The numeric code of the handle describes STDIN
0
keyboard entry
STDOUT
one
Output to the command prompt window
STDERR
two
Error output to command prompt window
UNDEFINED
3-9
Handles are defined separately by the application, and they are unique to each tool
The numbers 0 to 9 represent the first 10 handles. You can use the command Cmd.exe to run the program and redirect any of the first 10 handles to the program. To specify the handle to use, type the number of the handle before the redirect operator. If no handle is defined, the default
< 重定向输入操作符是 0,而默认的 >The redirect output operator is 1. Type in
< 或 >Operator, you must specify the read and write location of the data. You can specify a file name or other existing handle.
To specify a redirect to an existing handle, use the and (&) character, followed by the handle number to be redirected (that is, the & handle number). For example, the following command redirects handle 2 (or 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.
Redirect operator description >
Writes command output to a file or device, such as a printer, rather than to a command prompt window 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.
C:\ route.cfg
The > operator opens a specified file with write-only access. Therefore, you cannot use this operator to read a file. For example, if you start the program with the redirect operator > & 0, all attempts to write to handle 1 will fail because handle 0 is initially opened with read-only access.
Be careful
1 is the default handle for the > redirect output operator.
Copy handle
Redirect operator & you can copy output or input from one specified handle to another. For example, to send dir output to File.txt and error output to File.txt, type:
Dir > c:\ file.txt 2 > & 1
When you copy a handle, you can copy all the properties of the original state of the handle. For example, if a handle has a read-only access property, all copies of the handle have a read-only access property. You cannot copy a handle with a read-only access property to another handle with a write-only access property.
Use the & operator to redirect input and copy
To set the redirect input operator (& 1)
Use the > > redirect operator to attach output
To add output from the command to the end of the file without losing any information that already exists in the file, use two consecutive greater than signs (that is, >). For example, you can attach a list of directories generated by the dir command to an Dirlist.txt file using the following command:
Dir > > dirlist.txt
To append the output of the netstat command to the end of the Tcpinfo.txt, type:
Netstat > > tcpinfo.txt
Use the pipe operator (|)
The pipe operator (|) can extract the output of one command (STDOUT by default) and direct it to the input of another command (STDIN by default). For example, you can classify directories using the following command:
Dir | sort
In this case, both 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 takes the output of the dir command as input, and then sends the output to handle 1 (that is, STDOUT).
Merge commands with redirection operator
You can create custom commands by merging filter commands with other commands and file names. For example, you can use the following command to store a file name that contains 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 pipes (|) to separate the filters. For example, the following command searches each directory on disk C to find the file name that contains the "LOG" string and displays one screen at a time in the command prompt window:
Dir c:\ / s / b | find "log" | more
Using the pipe (|), you can direct Cmd.exe to send dir command output through the find filter command. The find command selects only file names that contain the string "LOG". The more command displays the file name selected by the find command (one screen at a time in the command prompt window). For more information about filter commands, see using filters.
Thank you for reading! This is the end of the article on "sample analysis of directional output in shell command". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.