How to solve the redirection problem in linux operation

PHPz
Release: 2023-05-17 14:37:14
forward
1768 people have browsed it

    1. Overview

    (1) Redirect command list

    ##command > fileRedirect output to filecommand < fileRedirect input to filecommand >> fileRedirect output to append The method redirects to filen > fileRedirects the file with file descriptor n to filen >> fileRedirect the file with file descriptor n to filen >& mMerge output files m and nn <& mMerge input files m and n<< tag Take the content between the start tag tag and the end tag tag as input
    Command Description

    (2) File description Symbol

    • 0: Usually standard input (STDIN)

    • 1: Standard output (STDOUT)

    • 2: It is the standard error output (STDERR)

    2. Output redirection

    (1) Command analysis

    command > file
    #执行command然后将输出的内容存入file。
    Copy after login

    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 ~]#
    Copy after login

    Note:

    There are two #s in many command lines in the above examples:

    • The first # means

      The current user is the root user (when it is another user, there will be a $ sign here) ;

    • The second # above means the meaning of

      comment .

    3. Input redirection

    1. Command parsing

    Unix commands can also get input from files, the syntax is:

    command < file
    #获取file文件中的内容作为输入内容,并用于commmand执行
    Copy after login

    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文件内容作为输入重定向计算行数了
    Copy after login

    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)。
    Copy after login

    4. In-depth understanding of redirection

    The standard input, standard output and standard error output have been briefly introduced in the above file descriptor, and will be explained in detail below.

    1. Introduction

    Generally, three files will be opened when each Unix/Linux command is run:

    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 commands

    command 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
    Copy after login

    5. Here Document

    Here Document is a special redirection method in Shell, used to redirect input to an interactive shell script or program.

    1. Grammar

    Its basic form is as follows:

    command << delimiter
        document
    delimiter
    #作用是将两个 delimiter 之间的内容(document) 作为输入传递给 command。
    Copy after login

    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 analysis

    EOF 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
    Copy after login

    EOF can also be customized as follows:

    [root@iZ2ze95cxr3kx9il409khtZ ~]# cat << CCC
    > a
    > b
    > c
    > d
    > CCC
    a
    b
    c
    d
    Copy after login

    When executing script input, you can use the following form:

    #拥有大量输入的时候可以用下面的形式,将标准输入的内容重定向到(输入到)test.sh文件中。
    [root@localhost ~]# cat << EOF >test.sh 
    > 123123123
    > 3452354345
    > asdfasdfs
    > EOF
    [root@localhost ~]# cat test.sh 
    123123123
    3452354345
    asdfasdfs
    [root@localhost ~]#
    Copy after login

    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!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template