Shell Learning 23 - Shell Input and Output Redirection

By default, Unix commands take input from the standard input device (stdin) and output the results to the standard output device (stdout) for display. Under normal circumstances, the standard input device is the keyboard, and the standard output device is the terminal, that is, the display.
Output Redirection
The output of a command can not only be on the display, but can also be easily diverted to a file, which is called output redirection.
The syntax for command output redirection is:
$ command > file
This way, output to the display can be redirected to a file.
For example, the following command will not see any output on the display:
$ who > users
Open the users file and you can see the following:
$ cat users
about 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
$
Output redirection will overwrite the file content, 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 take input from files. The syntax is:
command < file
In this way, commands that would otherwise need to get input from the keyboard are transferred to the file to read the content.
Note: Output redirection is greater than sign (>), input redirection is less than sign (<).
For example, to count the number of lines in the users file, use the following command:
$ wc -l users
2 users
$
It is also possible to redirect input to the users file:
$ wc -l < users
2
$
Note: The results of the two examples above are different: the first example, will output the filename; the second will not, because it only knows to read from standard input.
Redirection explained in depth
In general, 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 Unix programs output data to stdout by default.
Standard error file (stderr): The file descriptor of stderr is 2, and Unix programs 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 be redirected to file, you can write:
$command 2 > file
If you want stderr to be appended to the end of file, you can write:
$command 2 >> file
2 means the standard error file (stderr).
If you want to redirect stdout and stderr 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 redirection commands
Command Description
command > file redirects output to file.
command < file redirect input to file.
command >> file redirects output to file in append mode.
n > file Redirects the file with file descriptor n to file.
n >> file Appends the file with file descriptor n to file.
n >& m Merge output files m and n.
n <& m Merge input files m and n.
<< tag takes as input the content between the start tag tag and the end tag tag.
Here Document
Here Document currently has no unified translation, here is temporarily translated as "embedded document". Here Document is a special redirection method in Shell, and its basic form is as follows:
command << delimiter
document
delimiter
Its role is to pass the content (document) between the two delimiters as input to command.
Note:
The delimiter at the end must be written in the top box, and there cannot be any characters in front or behind, including spaces and tab indentation.
Spaces before and after the initial delimiter are ignored.
The following example counts the number of lines in the document with the wc -l command:
$wc -l << EOF
This is a simple lookup program
for good (and bad) restaurants
in Cape Town.
EOF
3
$
Here Documents can also be used 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 via 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 don't want the output to be displayed on the screen, you can redirect the output to /dev/null:
$ command > /dev/null
/dev/null is a special file and everything written to it is discarded; if you try to read from this file, nothing will be read. But the /dev/null file is very useful, redirecting the output of the command to it has the effect of "suppressing the output".
If you want to block stdout and stderr, you can write:
$ command > /dev/null 2>&1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325721819&siteId=291194637