Shell input/output redirection, shell output redirection

Source: Internet
Author: User

Shell input/output redirection, shell output redirection
Output redirection is generally achieved by inserting a specific symbol between commands. In particular, the syntax for these symbols is as follows:

command1 > file1
Run command1 in the preceding command and save the output content to file1. Note that existing content in any file1 will be replaced by new content. To add new content to the end of the file, use the> operator. The instance executes the following who command, which redirects the complete output of the command to the User File (users)
who > users
After execution, no information is output on the terminal, because the output has been redirected from the default standard output device (terminal) to the specified file. You can use the cat command to view the File Content
cat usersh tty7 2017-08-10 11:48 (:0)
The same as the output redirection, the Unix Command can also get the input from the file. The syntax is
command1 < file1
In this way, the command that needs to be obtained from the keyboard will be transferred to the file to read the content. Note: The output redirection is greater than the number (>), the input redirection is less than the number (<) instance, and then the above instance, we need to count the number of rows in the users file and execute the following command
$ wc -l users1 users
The wc (Word Count) command is used to Count the number of bytes, number of words, and number of rows in a specified file. The statistical result is displayed as follows:-l the number of statistical rows can also be redirected to the users file.
$  wc -l < users1 
Note: The results of the above two examples are different: In the first example, the file name is output; In the second example, the file name is not, because it only knows to read content from the standard input.
command1 < infile > outfile
Replace the input and output, execute command1, read the content from the file infile, and write the output to the outfile. In general, every Unix/Linux Command opens three files when running:
  • 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 want stderr to redirect to file, you can write as follows:
command 2 > file
If you want stderr to append to the end of the file, you can write as follows:
command 2 >> file
2 indicates the standard error file (stderr ). If you want to merge stdout and stderr and then redirect to file, you can write as follows:
$ Command> file 2> & 1 # Or $ command> file 2> & 1
If you want to redirect both stdin and stdout, you can write as follows:
command < file1 > file2
The command redirects stdin to file1 and stdout to file2Here using enthere Document is a special redirection method in Shell to redirect input to an interactive Shell script or program. 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 spaces and tab indentation. Spaces before and after the start of delimiter are ignored. The instance uses the wc-l command in the command line to calculate the number of lines in the Here Document:
$ Wc-l <EOF welcome to frethers www.cnblogs.com/freescienceEOF3 #3 rows of output result
We can also use the Here Document in a script, for example:
#! /Bin/bashcat <EOF welcome to freases www.cnblogs.com/freescienceEOF
Run the above script and output the result: Welcome to the frethers www.cnblogs.com/freescience/dev/null file. If you want to execute a command but do not want to display the output on the screen, you 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 disable output. If you want to block stdout and stderr, you can write as follows:
command > /dev/null 2>&1
Note: 0 indicates the standard input (STDIN), 1 indicates the standard output (STDOUT), and 2 indicates the standard error output (STDERR)

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.