Shell review---basic (input/output redirection)

    Most UNIX system commands take input from your terminal and send the resulting output back to your terminal. A command usually reads input from a place called standard input, which by default happens to be your terminal. Likewise, a command usually writes its output to standard output, which is also your terminal by default. The list of redirected commands is as follows:

 

Order illustrate
command > file Redirect output to file.
command < file Redirect input to file.
command >> file Redirect output to file in append mode.
n > file Redirect 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 opening tag tag and closing tag tag.

    It should be noted here that file descriptor 0 is usually standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR). Redirection is generally achieved by inserting specific symbols between commands. In particular, the syntax of these symbols is as follows:

 

command1 > file1

    The above command executes command1 and stores the output in file1. Note that any existing content in file1 will be replaced by the new content. If you want to add new content at the end of the file, use the >> operator. Execute the following who command, which redirects the complete output of the command to the users file (users):

 

$ who > users

    After execution, no information is output to the terminal because the output has been redirected from the default standard output device (terminal) to the specified file. You can view the file contents with the cat command:

 

$ cat users
_mbsetupuser console   Oct 31 17 : 35 tianqixin console Oct 31 17 : 35 tianqixin ttys000 Dec 1 11 : 33      

    Output redirection will overwrite the file content, see the following example:

 

$ echo "luyaran:www.luyaran.com"> users  $ cat users
luyaran :www .luyaran . com $

    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 "luyaran:www.luyaran.com">> users  $ cat users
luyaran :www .luyaran . com luyaran :www .luyaran . com $

    Like output redirection, Unix commands can also take input from files, the syntax is:

 

command1 < file1

    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 (<). Following the above example, we need to count the number of lines in the users file and execute 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.

 

command1 < infile > outfile

    Both input and output are replaced, command1 is executed, the contents are read from the file infile, and the output is written to outfile. Typically, 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或者 $ 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.

    Here Document is a special kind of redirection in Shell that redirects input to an interactive Shell script or program. 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. However, it should be noted that 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. Also, the spaces before and after the initial delimiter will be ignored.

    Count the number of lines in the Here Document with the wc -l command on the command line:

 

$ wc - l << EOF luyaran www .luyaran . com EOF 2 # The output is 2 lines $ 

    We can also use Here Document in scripts, for example:

 

#!/bin/bash
 cat << EOFluyaran www.luyaran.com EOF 

    Execute the above script and the output is:

 

luyaran 
www .luyaran . com

    If you want to execute a command but don't want the output 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 a command to it has the effect of "suppressing the output". If you want to block stdout and stderr, you can write:

 

$ command >/dev/null2>&1  

    Note, however, that 0 is standard input (STDIN), 1 is standard output (STDOUT), and 2 is standard error output (STDERR).

 

$ command > file 2>&1 $ command >> file 2>&1

    The & here has no fixed meaning. The & behind the > indicates that the redirected target is not a file, but a file descriptor. The built-in file descriptors are as follows:

 

1=> stdout 2=> stderr 0=> stdin   

    In other words , 2>1 means redirecting stderr to a regular file with file name 1 in the current path , while 2>&1 means redirecting stderr to a file with file descriptor 1 (ie /dev/stdout), which is It is the mapping of stdout in the file system. And &>file is a special usage, which can also be written as >&file, both of which have the same meaning and are equivalent to:

 

>file 2>&1

    Here &> or >& are regarded as a whole, and they have no separate meaning.

    Let's look at the sequence problem:

 

find / etc - name . bashrc > list 2 >& 1 # I would like to ask why the order can't be changed, like this find / etc - name . bashrc 2 >& 1 > list  

    This is in order from left to right. The first is to redirect the content to be output to stdout to a file. At this time, the file list is the stdout of the program, and then redirect stderr to stdout, which is the file list:

 

xxx > list 2>&1

The second is to redirect the content to     be output to stderr to stdout. At this time, a copy of stdout will be generated as the stderr of the program, and the content of the program to be output to stdout is still connected to the original body of stdout, so The second step redirects stdout, which has no effect on the copy of stdout:

 

xxx 2>&1> list 

    Well, the sharing ends here. If you feel good, please like and support more. . .

  Original link: https://blog.csdn.net/luyaran/article/details/80079677

Guess you like

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