Linux input and output redirection and pipeline (on)

Linux input and output redirection and pipeline (on)

0X00 Foreword

Redirection and pipelines are probably not unfamiliar to everyone, but it seems a bit difficult to be very organized. There is a feeling of being unable to say something in the mouth.
I checked on the Internet today and found that the online information is still quite messy. So today I plan to summarize and hang it out, I hope you can support it~
ps: In order to clarify that some Linux commands may be used, I will give a brief explanation. It may not be clear. If you don’t understand, you can check it elsewhere. data. Don't talk nonsense, just start!

0X10 output redirection

1.'>' redirection character

We know that when entering certain commands in the Linux terminal, the results will be output on the screen.
For example, we are familiar with the cat command to output the contents of the file to the screen:

cat notes.csv

The content of the file
Insert picture description here
will be displayed on the screen at this time: So the question is, what if I don't want to display the information on the screen? But want to save it in a file?
The answer is very simple, use the redirection symbol-' > '

cat notes.csv > new_notes.csv

Finished? That's it! Just one symbol, done! Let's take a look at the implementation effect:
Insert picture description here
some people want to ask, it feels like there is nothing wrong. Indeed, because the screen that was supposed to be printed was redirected to new_notes.csv. Let's take a look at the content of new_notes.csv now. Right
Insert picture description here
, it's all in!
Summarize the usage of'>':

  • If the file to be redirected to (new_notes.csv) does not exist, a file will be created

  • If the file exists, it will directly overwrite the contents of the file

    Therefore, everyone must be careful, otherwise it is easy to regret heartbroken!
    Then someone asked again, this is too bad, what if I don't want to cover the content.
    This is really enough, the designers of Linux have long thought of it.

2. ">>" redirection

In order to solve the above problem, just use the " >> " redirection symbol. It has the same function as' > ', except for one thing: the
output stream will redirect the end of the file instead of overwriting all the content

Let's run the cat command twice and adopt >> redirection:

cat notes.csv >> new_notes.csv
cat notes.csv >> new_notes.csv

Let's take a look at the content
Insert picture description here
in new_notes.csv. The results of two cats are redirected to new_notes.csv. So the content of >> is connected to the end of the file.

When you get here, some people may still have questions. It feels like this is useless. Just like the cat command, it originally inputs the information on the screen. Wouldn't it be self-defeating if you get it into the file?

Okay, let's get the whole hardcore!

3. A hardcore example

Let's recognize a command: cut, as the
name suggests, is to cut out some information in the file and output it to the screen.
Let's take a look at the following two parameters:

  • -d According to a certain symbol (comma, semicolon, double quotes, etc.) to divide the area

  • -f take the area, the area divided according to the symbol

    Take the notes.csv file above as an example,
    execute the following command:

cut -d , -f 1 notes.csv

Explain, press',' to divide the area, take the first area (ie the first column) and the
result:
Insert picture description here
Is it a bit magical?
Then this is still displayed on the screen, such information is extracted or not, it can only be useful if it is stored. At this time, the role of the redirection symbol'>''>>' is displayed.
Let's code a few instructions first:

cut -d , -f 1,3 notes.cvs >> news_notes.csv
cut -d , -f 1-3 notes.cvs >> news_notes.csv
cut -d , -f 2- notes.cvs >> news_notes.csv
这三条指令,前面的不用说,按 ',' 来划分区域,后面的意思分别为:

Column 3 taken
take 1-3
taken to the last column 2

Look at the news_notes.csv file again: the
Insert picture description here
three parts correspond to three instructions, so the information you want to get is cut out and stored. Isn’t it:
Insert picture description here
Let’s just nag, what if I neither want to output to the screen, nor to a file?
(There are so many requirements) Remember, this is Linux! Only you can't think of it, you can't do it without it! (A bit exaggerated)
There is a file commonly known as "black hole" in Linux, which is

/dev/null

The /dev/null file is a special file, not a directory. This file has a unique attribute: it is always empty. It can invalidate any data sent to /dev/null, as if the data fell into a bottomless black hole.

Therefore, if we don't need to display the result of the cut command just now in the terminal and don't want to store it in a file, we can do this:

cut -d , -f 1 notes.csv > /dev/null

Then, there is no Then 23333
sent a picture:
Insert picture description here

0X20 "2>, 2>>, 2>&1": redirect error output

1. stdin, stdout, stderr: standard input, standard output, standard error output

We first need to understand these terms: standard input, standard output, standard error output. It
is not difficult:

Standard input: stdin, input data from the keyboard to the terminal
. Standard output: stdout. Refers to the terminal output information (not including error information)
standard error output: stderr. Refers to the error message output by the terminal

Needless to say, the standard input, the standard output is what we showed in cat before, so what about the standard error output?
For example, we enter:

cat tmp.csv

The following message will appear: It
Insert picture description here
shows that there is no such file, this is the standard error output.
Not much to say, just flip the chart.
Insert picture description here
But the question we want to discuss is: Can'>' and ">>" act on the standard error message?
Let's try it:

cat tmp.csv > new.txt

Result: I
Insert picture description here
found that the screen showed an error message, and then checked new.txt and found that it was empty. So the error message is not redirected to new.txt, so:
'>' and ">>" cannot redirect the error message

2. Use of 2>, 2>>

Therefore, in order to meet the requirements of the majority of Linuxers, the designer also designed a redirection symbol for standard error messages.
Let's first look at the following table:

File descriptor first name Explanation
0 stdin Standard input
1 stdout Standard output
2 stderr Standard error output

What is a file descriptor is not explained in detail, we now know that 2 represents standard error output.

Now
let ’s look at how to indicate the redirection symbol of the standard error message . You may not believe it, just add 2 in front of the standard redirection symbol.

2>
2>>

Let's change the command just now:

cat tmp.csv 2> new.txt

Look at the result:
Insert picture description here
At this time, the previous redirection is not wrong, but the error message has been written into the new.txt file. The
same is true for 2>> , but it means that the information is added to the end of the file, so I won’t repeat it. Up

3. Use of 2>&1

Until now, someone may have to say again, if I want to redirect both standard output and standard error output to the same place. How to do it
(Too capricious)
Insert picture description here
Order!

cat not_exist_file.csv > results.txt 2>&1

The function of the above command is to redirect all the output (standard output and standard error output) of the cat not_exist_file.csv command to the results.txt file
because the file not_exist_file.csv does not exist, but because 2>&1 is added This symbol, so the standard output (empty) and standard error output (cat: not_exist_file.csv: No such file or directory) are redirected to the results.txt file.
Insert picture description here
This may not be seen much, but sometimes it is just a certain When using some commands, there will be standard output and error output. For example, when the file is found again, there will be insufficient permissions

Then the question is again: What if you don't want to overwrite, but connect to the end of the file? Is it 2>>&1
wrong! 2>&1 cannot be changed. What needs
to be changed is to
store the information at the end of the file :

cat not_exist_file.csv >> results.txt 2>&1
最后上图:

Insert picture description here >> and 2>> are not added here, otherwise the picture is too complicated

0X30 To be continued...

I’m tired of writing today, let’s write tomorrow, hehehe
next time I talk about input redirection and pipes
Insert picture description here

The next part is also finished, hanging here, I hope you can support more:
Linux input and output redirection and pipeline (below)
——————————————————————— —————————————————————————————————
Reference: Linux Command Line and Shell Script Programming Guide/23 Stream and Output Redirection , The direction of the heart

Guess you like

Origin blog.csdn.net/rjszz1314/article/details/104417444