(1) Redirect command list
Command | Description |
---|---|
Redirect output to file | |
Redirect input to file | |
Redirect output to append The method redirects to file | |
Redirects the file with file descriptor n to file | |
Redirect the file with file descriptor n to file | |
Merge output files m and n | |
Merge input files m and n | |
Take the content between the start tag tag and the end tag tag as input |
(2) File description Symbol
(1) Command analysis
command > file #执行command然后将输出的内容存入file。
Note:
The content in the file will be replaced by the new content. If you don’t want to replace everything but append to the end of the file, use > ;> operator.(2) Example analysis
[root@localhost ~]# w 20:41:36 up 55 days, 5:17, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 223.166.20.53 20:24 0.00s 0.05s 0.00s w [root@localshost ~]# w > users #w命令执行后的结果输出到users文件中 [root@localshost ~]# ll -rw-r--r-- 1 root root 204 Jan 3 20:41 users [root@localshost ~]# cat users #查看users文件内容,正是w命令执行后的输出结果 20:41:58 up 55 days, 5:17, 1 user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 223.166.20.53 20:24 6.00s 0.05s 0.00s w [root@localshost ~]# who root pts/0 2021-01-03 20:24 (223.166.20.53) [root@localshost ~]# who > users #将who命令执行结果输出重定向到users文件 [root@localshost ~]# cat users #发现users文件中原先w命令的输出内容被who命令覆盖了 root pts/0 2021-01-03 20:24 (223.166.20.53) [root@localshost ~]# echo "Hello world" >> users #使用>>操作符则会追加在后面输出 [root@localshost ~]# cat users root pts/0 2021-01-03 20:24 (223.166.20.53) Hello world [root@localshost ~]#
Note:
There are two #s in many command lines in the above examples:The current user is the root user (when it is another user, there will be a $ sign here) ;
comment .
command < file #获取file文件中的内容作为输入内容,并用于commmand执行
Note:
The output redirection is the greater than sign (>), and the input redirection is the less than sign (<). 2. Example analysis[root@localhost ~]# wc -l users 2 users #wc指令可以计算文件的Byte数、字数、或是行/列数,若不指定文件名称、或是所给予的文件名为"-",则wc指令会从标准输入设备读取数据。 [root@localhost ~]# wc -l < users 2 #将输入重定向到 users 文件,上面命令作用就是将users文件内容作为输入重定向计算行数了
Note:
The first example will output the file name; The second one No, because it only knows to read from standard input.command < infile > outfile #同时替换输入和输出,执行command,从文件infile读取内容,然后将输出写入到outfile中。 #理解成,从标准输入中获取内容(输入重定向到infile)作为标准输出(输出重定向到outfile)。
1) Standard input file (stdin) :
The file descriptor of stdin is 0, and Unix programs read data from stdin by default.2) Standard output file (stdout):
The file descriptor of stdout is 1, and Unix programs output data to stdout by default.3) Standard error file (stderr):
The file descriptor of stderr is 2, and Unix programs will write error information to the stderr stream. By default,command > file redirects stdout to file, command < file redirects stdin to file.
2. Detailed explanation of commandscommand 2>file #stderr 重定向到 file command 2>>file #stderr 追加到 file 文件末尾 command > file 2>&1 command >> file 2>&1 #stdout 和 stderr 合并后重定向到 file command < file1 >file2 #对 stdin 和 stdout 都重定向 #command 命令将 stdin 重定向到 file1,将 stdout 重定向到 file2
command << delimiter document delimiter #作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。
Note: The delimiter at the end of
must be written in top case , there cannot be any characters in front of it, nor can there be any characters in the back, including spaces and tab indentations. The spaces before and after the starting delimiter will be ignored. 2. Example analysisEOF is the abbreviation of END Of File, which represents a custom terminator. Since it can be customized, EOF does not have a fixed value and you can set an alias at will. For example, in Linux, pressing Ctrl-D can be used as EOF instead. EOF usually works with cat to output multi-line text.The examples are as follows:
[root@localhost ~]# wc -l << EOF > a > b > c > d > e > EOF 5 #输入内容为5行 [root@localhost ~]# cat << EOF > a > b > c > d > e > f > EOF a b c d e f
[root@iZ2ze95cxr3kx9il409khtZ ~]# cat << CCC > a > b > c > d > CCC a b c d
#拥有大量输入的时候可以用下面的形式,将标准输入的内容重定向到(输入到)test.sh文件中。 [root@localhost ~]# cat << EOF >test.sh > 123123123 > 3452354345 > asdfasdfs > EOF [root@localhost ~]# cat test.sh 123123123 3452354345 asdfasdfs [root@localhost ~]#
The above is the detailed content of How to solve the redirection problem in linux operation. For more information, please follow other related articles on the PHP Chinese website!