The terminal realizes the reading and writing of the data stream in the file;
The list of REDIRECT commands is as follows:
Command |
Description |
Command > File |
Redirects the output to file. Write terminal data to file |
Command < file |
Redirects the input to file. The file |
Command >> file |
Redirects the output to file in an append manner. |
n > File |
redirect files with file descriptor N to file. |
n >> File |
Files with file descriptor n are redirected to file in an append manner. |
N >& m |
Merges the output file m and N. |
N <& m |
Merges the input file m and N. |
<< tag |
Enter the contents of the tag between tag and end tag tags as input. |
Note that the file descriptor 0 is typically standard input (STDIN), 1 is the standard output (STDOUT), and 2 is the standard error output (STDERR). This can be used for log printing to a file;
Output redirection
Redirects are typically implemented by inserting specific symbols between commands. In particular, the syntax for these symbols is as follows:
> file1
The above command executes Command1 and then stores the output in File1.
Note that any content that already exists within the file1 will be replaced by new content. If you want to add new content to the end of the file, use the >> operator.
Instance
Execute the following who command, which redirects the full output of the command to the user file (users):
> Users
After execution, there is no output information in 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 contents of a file:
$ cat Users_mbsetupuser Console Oct:Oct:Dec 1:
Output redirection overwrites the file contents, see the following example:
"Rookie Tutorial: www.runoob.com"> users$ cat users Novice Tutorial:www. Runoob. com$
If you do not want the file content to be overwritten, you can use >> to append to the end of the file, for example:
"Rookie Tutorial: www.runoob.com">> users$ cat users Novice Tutorial:www. Runoob. COM rookie tutorial:www. Runoob. com$
Input redirect
As with output redirection, Unix commands can also get input from a file with the following syntax:
< file1
In this way, commands that would otherwise need to get input from the keyboard are transferred to the file read content.
Note: Output redirection is greater than sign (>), and input redirection is less than sign (<).
Instance
Next, we need to count the number of rows in the users file and execute the following command:
-L users 2 users
You can also redirect the input to the users file:
$ -< users 2
Note: The results of the above two examples are different: The first example outputs the file name, and the second one does not because it only knows what to read from the standard input.
<> outfile
Replace the inputs and outputs at the same time, execute Command1, read the contents from the file infile, and write the output to outfile.
Redirect in-depth explanation
In general, each Unix/linux command will open three files when it is run:
- Standard input file (stdin): stdin file descriptor for 0,unix program reads data from stdin by default.
- Standard output file (stdout): StdOut file descriptor for 1,unix program outputs data to stdout by default.
- Standard error file (stderr): The stderr file descriptor for the 2,unix program writes an error message to the stderr stream.
By default, command > file redirects stdout to File,command < file to redirect stdin to file.
If you want stderr to be redirected to file, you can write:
2> file
If you want stderr to append to the end of file, write this:
2>> file
2 indicates a standard error file (stderr).
If you want to redirect stdout and stderr to file after merging, you can write:
>2>&1 or >>2>&1
If you want to redirect both stdin and stdout, you can write this:
<>file2
The command commands redirect stdin to File1, redirecting stdout to File2.
Here Document
Here Document is a special redirection in the shell used to redirect input to an interactive shell script or program.
Its basic form is as follows:
<< delimiter documentdelimiter
Its role is to pass the contents (document) between the two delimiter as input to the command.
Attention:
- The end of the delimiter must be shelf write, the front can not have any characters, the back can not have any characters, including spaces and tab indentation.
- The space before and after the beginning of the delimiter is ignored.
Instance
The number of rows in here Document is computed by the wc-l command at the command line:
-<< EOF Welcome to Rookie Tutorial www. Runoob. Comeof3# Output of 3 rows $
We can also use here Document in a script, for example:
#!/bin/bash# Author: Rookie Tutorial # url:www.runoob.com<< EOF Welcome to Rookie Tutorial www. Runoob. comeof
Execute the above script and output the result:
Welcome to the Rookie tutorial www. Runoob. COM
/dev/null file
If you want to execute a command, but you do not want the output to appear on the screen, you can redirect the output to/dev/null:
>/dev/null
/dev/null is a special file, and the content written to it is discarded, and if you try to read from the file, nothing is read. However, the/dev/null file is very useful and redirects the output of the command to it, which results in a "no output" effect.
If you want to block stdout and stderr, you can write this:
>/dev/null2>&1
Note:0 is the standard input (STDIN), 1 is the standard output (STDOUT), and 2 is the standard error output (Stder
Article Source: http://www.runoob.com/linux/linux-shell-io-redirections.html
Linux shell terminal read-write file data stream and redirect >,<,<<,>>