Shell input and output redirection

Source: Internet
Author: User
Tags stdin



The Unix command obtains input from the standard input device (stdin) By default, outputting the result to the standard output device (STDOUT) display. In general, the standard input device is the keyboard, the standard output device is the terminal, that is, the display.


Output redirection


The output of the command can not only be a monitor, but it can also be easily transferred to a file, which is called output redirection.

The syntax for command output redirection is:


    1. $ Command > file


In this way, the contents of the output to the display can be redirected to the file.

For example, the following command will not see any output on the monitor:


    1. $ who > users


Open the users file and you can see the following:

$ cat users
oko tty01 Sep 12 07:30
ai tty15 Sep 12 13:32
ruth tty21 Sep 12 10:10
pat tty24 Sep 12 13:07
steve tty25 Sep 12 13:03
$
The output redirection will overwrite the contents of the file, please see the following example:

$ echo line 1> users
$ cat users
line 1
$
If you don't want the file content to be overwritten, you can use >> to append to the end of the file, for example:

$ echo line 2 >> users
$ cat users
line 1
line 2
$
Input redirection
Like output redirection, Unix commands can also get input from a file, the syntax is:

command <file
In this way, the command that originally needed to get input from the keyboard will be transferred to the file to read the content.

Note: Output redirection is greater than sign (>), input redirection is less than sign (<).

For example, to calculate the number of lines in the users file, you can use the following command:

$ wc -l users
2 users
$
You can also redirect input to the users file:

$ wc -l <users
2
$
Note: The results of the above two examples are different: the first example will output the file name; the second will not because it only knows what to read from standard input.

Redirect in depth
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, Unix programs output data to stdout by default.
Standard error file (stderr): The file descriptor of stderr is 2, and the Unix program will write error information 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:

$ command 2> file
If you want to append stderr to the end of the file, you can write:

$ command 2 >> file
2 means standard error file (stderr).

If you want to merge stdout and stderr and redirect to file, you can write:

$ command> file 2> & 1
or

$ command >> file 2> & 1
If you want to redirect both stdin and stdout, you can write:

$ command <file1> file2
The command command redirects stdin to file1 and stdout to file2.


List of all available redirect commands
Command Description
command> file redirects output to file.
command <file redirects input to file.
command >> file redirects the output to file in an appended manner.
n> file redirects the file with file descriptor n to file.
n >> file redirects the file with file descriptor n to file in append mode.
n> & m merges the output files m and n.
n <& m merges the input files m and n.
<< tag takes the content between the start tag and end tag as input. Here Document
There is currently no unified translation for Here Document, which is temporarily translated as "embedded document". Here Document is a special redirection method in Shell. Its basic form is as follows:

command << delimiter
document
delimiter
Its role is to pass the content (document) between two delimiters as input to the command.

note:

The delimiter at the end must be written in the top grid. There must be no characters in front and no characters in the back, including spaces and tab indentation.
The spaces before and after the initial delimiter will be ignored.

The following example uses the wc -l command to calculate the number of document lines:

$ wc -l << EOF
    This is a simple lookup program
    for good (and bad) restaurants
    in Cape Town.
EOF
3
$
You can also use Here Document in scripts, for example:

#! / bin / bash
cat << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
operation result:

This is a simple lookup program
for good (and bad) restaurants
in Cape Town.

The following script saves the document to the test.txt file through the vi editor:

#! / bin / sh
filename = test.txt
vi $ filename << EndOfCommands
i
This file was created automatically from
a shell script
^ [
ZZ
EndOfCommands
Run the script:

$ sh test.sh
Vim: Warning: Input is not from a terminal
$
Open test.txt, you can see the following content:

$ cat test.txt
This file was created automatically from
a shell script
$
/ 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 contents written to it will be discarded; if you try to read from the file, nothing will be read. But the / dev / null file is very useful, and redirecting the output of the command to it will have the effect of "disabling output".

If you want to block stdout and stderr, you can write:

Plain text copy
$ command> / dev / null 2> & 1
Shell input and output redirection 

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.