[Shell] shell input/output redirection

Source: Internet
Author: User

By default, Unix Commands obtain input from the standard input device (stdin) and output the result to the standard output device (stdout) for display. Generally, the standard input device is the keyboard, and the standard output device is the terminal, that is, the display.

Generally, three files are opened when each Unix/Linux Command is run:

    • Standard input file (stdin): The file descriptor of stdin is 0, and Unix programs read data from stdin by default.
    • Standard output file (stdout): The file descriptor of stdout is 1, and the UNIX program outputs data to stdout by default.
    • Standard Error file (stderr): The file descriptor of stderr is 2. UNIX programs write error messages to the stderr stream.
By default, command> file redirects stdout to file, and command <file redirects stdin to file.

    • If you wantStderr redirects to fileCan be written as follows: Command 2> File
    • If you wantStderr append to the end of FileCommand 2> file; 2 indicates the standard error file (stderr ).
    • If you wantMerge stdout and stderr and redirect to fileCan be written as follows: Command> file 2> & 1 or command> file 2> & 1
    • If you wantStdin and stdout are both redirected.Command <file1> file2; command redirects stdin to file1 and stdoutFile2.
  • Command> File Redirects the output to file.
  • Command <File Redirects the input to file.
  • Command> File Redirects the output to file in append mode.
  • N> File Redirects a file whose file descriptor is n to a file.
  • N> File Redirects an object whose file descriptor is n to a file in append mode.
  • N> & M Merge the output files M and N.
  • N <& M Merge the input files M and N.
  • <Tag The content between the start tag and end tag is used as the input.

Input redirectionCommand output can be not only a display, but also easily transferred to a file, which is called output redirection.
Command output redirection Syntax: Command> file, The content output to the monitor can be redirected to a file.
 
#! /Bin/bash # The output redirection syntax is: command> filewho> Users # output redirection overwrites the file content cat usersecho -------------------- echo line 1> userscat users echo ~~~~~~~~~~~~ Echo line 2> Users # If you do not want the file content to be overwritten, you can use> append to the end of the file cat users # output # centos tty1 (: 0) # centos pts/0 (: 0.0) # ---------------------- # Line 1 #~~~~~~~~~~~~ # Line 1 # Line 2
The same as the output redirection, the Unix Command can also get the input from the file. The syntax is as follows: Command <file, Commands that need to be obtained from the keyboard will be transferred to the file to read the content.
#! /Bin/bash # input redirection: Command <file. In this way, the command that needs to be obtained from the keyboard will be transferred to the file to read the content. # Calculate the number of rows in the users file WC-l users #=> will output the file name, WC-l command to calculate the number of rows of Document # output 2 userswc-L <users #=> no output file name # output 2 # first example, output file name; second won't, because it only knows to read content from standard input.
Embedded document Currently, there is no uniform translation for here document, which is translated as "embedded document". Here document is a special redirection method in shell. Its basic form is as follows:
 
Command <delimiter documentdelimiter
It is used to pass the content (document) between two delimiter to the command as input.
Note:
Delimiter at the end must be written in the top level. There cannot be any character at the front and no character at the end,Including space and Tab indentation.
Spaces before and after the start of delimiter are ignored.
#! /Bin/bash WC-L <EOF this is a simple lookup program for good (and bad) When ants in Cape Town. EOF # output 3cat <eofthis is a simple lookup programfor good (and bad) Export antsin Cape Town. EOF # output # This is a simple lookup Program # For good (and bad) without ants # in Cape Town.
/Dev/null File If you want to execute a command Do not want to display output results on the screenYou can redirect the output to/dev/null: Command>/dev/null.
/Dev/null is a special file, and the content written to it will be discarded. If you try to read the content from the file, nothing can be read. However, the/dev/null file is very useful. Redirecting the command output to it will "prohibit output.
If you want to block stdout and stderr, you can write: Command>/dev/null 2> & 1
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.