Linux Basic Tutorial: 5. Preparations before getting started with the shell

Linux still likes the shell statement, which is very similar to the C language shell. Before learning, we must first understand the shell and expand some necessary knowledge:

1. Redirection, pipes, connectors and text analyzers (awk)

So now we start to talk about some more advanced linux commands, the first is redirection, what is redirection?

Redirection is to redefine the input and output of the file to a new location. There are three types of redirection: output redirection (> or >>), input redirection (<), error redirection (2> or 2>>) .

The >> and << here represent additional data, and < and > represent all replacements. To understand redirection, we now need to understand three types of input in linux:

Standard input: the label of the input file is 0, that is, 0 represents the standard input, the default is the keyboard, and the data of the input file is read when the command is executed;

Standard output : the label is 1, the default device is the monitor, and the input result is sent to the standard output file after the command is executed;

Standard error: the label is 2, and the default device is also a monitor. After the command is executed, the error message will be sent to the standard error file;

So that's why error redirection is just putting a 2 in front of input redirection;

1. Output redirection:

ls >> 文件名

 

This command is to output the result of ls execution to the standard.c file. It can be clearly seen that the results of the files and folders under our current folder are displayed in the content of the standard.c file; similarly, by analogy, if we Whether it is the ll command or other commands with output functions, the results are output to the following files;

2. Input redirection

Input redirection is the reverse, we can use the content of a certain file as the input of a command, such as some commands that need to be input, grep, cat:

grep pro < 文件名

 

Here is to grab the content with pro characters in the standard file;

3. Error redirection

This is actually used for grabbing errors. The usage is similar to output redirection. We must first have an error program, such as the following error code:

 

I wrote this code in the lesson01.c file. In fact, there is no semicolon at the end, and then an error will be reported when compiling. We can output the error message to the standard.c file by using >> to append the input:

gcc -c 要编译的文件 2> 输出的文件

 

Here you can see that there are more error messages in the standard.c file when compiling the lesson01.c file;

4. Pipeline

The pipe symbol is |, its function is to use the output of one command as the input content of another command, such as the combination of grep command and cat command:

cat 文件 | grep 关键字

 

What is implemented here is to use the content of cat as the grabbing object of grep. It is obvious that a record with init is entered here, which is actually a record in /etc/passwd;

5. Connector

There are three kinds of connectors: ; (semicolon), && (logical and), || (logical or)

semicolon;:

Because sometimes we want to execute a lot of commands, if we execute them one by one, it is too cumbersome, why not write a bunch of them at once and let them execute slowly by themselves:

命令1;命令2;

 

This can receive many commands, but they are all executed sequentially, that is, they will be executed regardless of whether they fail or not;

Logical AND &&:

命令1&&命令2

 

This statement has the ability to judge. Only after the previous statement is executed successfully, the following statement will be executed. It can be seen that we will execute ls after executing ll successfully, but will not execute ls after executing s fails;

Logical OR ||:

This is actually the opposite of the logical AND, that is, after the previous one fails, the latter one will be executed:

命令1 || 命令2

 

It can be seen that the second instruction is not executed after the first instruction is executed;

6. Text analyzer awk

This command is similar to grep, and it is used to grab the data we want, but this command has more options, better functions, and more powerful. Let's look at its rules:

awk -F 分隔符 '条件{printf 列和格式}' 文件

The meaning here is that we are in a file, and its content is like this:

 

The meaning of the separator is like we take the boundary of the file. For example, here we can remove the space. If there is a; between Xiaoming and Mathematics, we can take the semicolon. If not set, that is, the default is a space by default, so Divide the data in the first row into three columns, that is, Xiaoming, Mathematics and 89, which are represented by $1, $2, and $3 respectively. If there is data in the back, and so on, we can set the conditions ( $2=="Mathematics "), like this, means to filter out the records whose second column is mathematics, and the format is the character style we want to add:

 

You can see that the content we want is output here;

2. The first shell program

Shell script is a plain text file commonly used in linux. It can be used to execute some shell programs we want to execute. Let's get started before learning;

Create a hello.sh file and enter:

 

Here # is the meaning of comment, echo is the meaning of promise, date is the printing time, write the file and save it and then execute it in three ways:

sh 文件名
bash 文件名
./文件名 (前提是需要使用chmod更改文件为可执行文件)

 

Generally speaking, bash is more commonly used than sh, because it is too troublesome to change permissions, and there are many ways to execute files, so I will not introduce them one by one here, and interested friends can try it by themselves;

Guess you like

Origin blog.csdn.net/aiwanchengxu/article/details/127549227