Linux learning [17] bash learning in-depth 3 --- wildcards & special symbols --- data flow redirection

Preface

This blog is a compilation of some wildcard characters (wildcards in bash) that were involved in previous searches. This lays a foundation for later pipeline related work.


1. Wild characters

Here is a table, followed by relevant examples.

symbol meaning
* Represents any character from "0 to infinite"
Represents "there must be one" any character
[ ] It also means "there must be one character in the brackets" (not any character). For example, [abcd] means "there must be one character, which may be any one of a, b, c, d"
[ - ] If there is a minus sign within square brackets, it means "all characters in the encoding sequence". For example, [0-9] represents all numbers between 0 and 9, because the language encoding of numbers is continuous!
[ ^ ] If the first character within the square brackets is an exponent symbol (^), it means "reverse selection". For example, [^abc] means that there must be one character, and any character other than a, b, c will be accepted.

Example:
Take /etcthe directory as an example.
After cd into the /etc directory, you can see a lot of files through ls, and some of the file prefixes overlap, which is convenient for us to demonstrate.
Insert image description here
The above picture is not all listed, there are many documents below.

例1:We ls h*represent files starting with h, and any file with h followed by 0/infinite characters will work.
Here we can see that listed are some files starting with h and files starting with hp in the hp directory. They all meet the requirements of files starting with h, but Ubuntu classifies them into categories, indicating whether different files are in the current directory or the subdirectory below.
Insert image description here

例2:Now we need to find the ones that start with host, and there must be one character after the host.
instruction:ls host?

Insert image description here

Insert image description here
There are many names starting with host, but the ones with only one character after host are only the hosts file, so ls host?of course the result of the search is only the hosts file.

例3:Now search for files starting with h, and there must be a character file after h.
We can see through ls that there is a hp subdirectory in the /etc directory.
Insert image description here
When we use it ls h?, it does not display the hp directory, but directly displays the files in hp. This is slightly different from Example 2, so it’s worth noting.
Insert image description here

例4:List the files in the /etc directory that start with any one of the three characters [wzy] and have no restrictions on the following characters.
Instruction: ls [wzy]*
Insert image description here
例5:List the files in the /etc directory that start with the characters x->z and have no limit on the following characters.
If it is a directory, all files in the corresponding directory will be listed.
Instructions: ls [x-z]*
Insert image description here
例6:The same effect as Example 5, but in reverse sampling mode.
Instruction: ls [^a-w]*
This means listing files starting with other letters except a->w. Except for a->w, it means starting with xyz, so the effect is the same as Example 5 above.

Insert image description here

2. Special characters

In addition to the wildcard characters above, bash also has many special characters.
A table is listed below for easy reference.

symbol content
# Annotation symbol: This is most commonly used in scripts and is regarded as a description! The following data will not be executed
\ Escape symbols: Restore "special characters or wildcards" to normal characters
| Pipeline: the definition that separates two pipeline commands (introduced in the next two sections);
; Continuous command issuance delimiter symbol: definition of continuous command (note! It is not the same as pipeline command)
~ User's home folder
$ Get the prefix character of the variable: that is, the variable replacement value that needs to be added before the variable
& Job control: turning instructions into work in the background
! "Not" in the sense of logical operations means not!
/ directory symbol: path-separated symbol
>, >> Data flow redirection: output direction, namely "replacement" and "accumulation"
<, << Data flow redirection: input orientation (these two will be introduced in the next section)
’ ’ Single quotes do not have the function of variable substitution ($ becomes plain text)
" " Has the function of variable substitution! ($ can retain related functions)
`` The middle of the two ` is the instruction that can be executed first, you can also use $ ( )
( ) In the middle are the start and end of the subshell
{ } In the middle is the combination of command blocks!

3. Data flow redirection

3.1 Standard output

This word sounds very high-end. To put it simply, it means that the things I output are stored in a different place.
For example, if we used lsthe command to view all files in the current directory, the console will output it directly to the console interface.
If we want to import the output content into the first file we create, for example, I /home/edwinwzy/LearnOfLinuxcreate an empty file in a directory datatre, and
I want to /etcstore the content output by the ls command in the directory into datatre. I can do this:ls >/home/edwinwzy/LearnOfLinux/datatre

There is no need to create the datatre file here, because when we use the command, if there is no such file, the system will automatically create it and save the data to the file.
The left side of the picture below is the instruction, and the right side is the data in the file.

Insert image description here

The above is the conventional application of data flow redirection. The following is a detailed introduction to data flow redirection based on the content in the book.

When we execute an instruction, the instruction may read data from a file, process it, and then output the data to the screen. Usually data information includes standard output and error output. Standard output and standard error output represent "standard output (STDOUT)" and "standard error output (STDERR)" respectively. These two things are output to the screen by default. Standard output refers to "the correct message returned by the command execution", while standard error output can be understood as "the error message returned after the command execution fails."

For example, we use ls to view the file starting with habcd, but in fact there is no such file in our /etc directory. At this time, it will prompt that the file does not exist, then this is the standard error output. On the contrary, if there is a file that symbolizes the condition of our command, the file will be displayed, and the display of this file is the standard output.

Standard input (stdin): code is 0, use < or <<;
standard output (stdout): code is 1, use > or >>;
standard error output (stderr): code is 2, use 2> or 2>> ;

The usage has been demonstrated at the beginning of this section, but what is the difference between > and >>, and how to use them, are explained below.
>Defaults to standard output, not standard error.

1>: Output the "correct data" to the specified file or device
by overwriting; 1>>: Output the "correct data" to the specified file or device by accumulation;
2>: Overwrite The method will output "wrong data" to the specified file or device;
2>>: The method of accumulation will output "wrong data" to the specified file or device;

If we want to put the output of multiple instructions into the same file, accumulation and coverage will be involved. The usage is listed above. The usage is relatively simple and will not be demonstrated here.

下面还有几个用法:

What if I know an error message will occur, so I want to ignore it without displaying or saving it? At this time, black hole equipment /dev/nullis very important! This /dev/null can eat any information directed to this device.

1. Discard the incorrect data and display the correct data on the screen

find /home -name .bashrc 2> /dev/null

2. Both correct and incorrect data are saved in a file.

find /home -name .bashrc > list 2> list -------错误写法

find /home -name .bashrc > list 2>&1 -----正确写法

Why is the wrong way of writing wrong? Although it can indeed store correct and incorrect data in a unified manner, since the two streams of data are written to one file at the same time and no special syntax is used, the two streams of data may be interleaved and written into the file, causing the order to be disordered. . So although the list file will still be generated in the end, the data arrangement in it will be strange, instead of the original output sorting on the screen.

3.2 Standard input

What is written above all outputs data through instructions and then stores the data in a file.
The standard input is the reverse process, that is, the data originally input from the console becomes data read from the file.

It should be easy to understand with an example.
I use it in the current directory cat >data. If there is no data file, it will be automatically created. Then enter 123456 and press ctrl+c.
At this time, a file containing 123456 texts named data is created.
Insert image description here

Now I want to create a DATA file, the data content inside is the data of data.
Insert image description here
Insert image description here

The above example is just a demonstration, and actual application requires further practice.


Summarize

This blog sorts out universal characters and special symbols, gives examples of the use of the former, and elaborates on data flow redirection, laying a foundation for subsequent learning of pipelines and other topics.

Guess you like

Origin blog.csdn.net/Edwinwzy/article/details/131314911