[shell]—How to understand the addition of "2>&1" and other redirection characters after calling a shell script


1. Prospects

Calling the script in the shell to run in the background will write the running log file into a .log file. 2>&1 is often added after the .log , and finally **&** means to execute this command in the background.

nohup sh /root/model_name/script/black_table_stat.sh > /root/model_name/log/black_table_stat_1_$(date +"%Y%m%d%H").log 2>&1 &

Second, understand the meaning of 0, 1, 2 in linux

name the code operator
standard input 0 < 或 <<
standard output 1 >,>>,1>或1>>
standard error output 2 2> or 2>>

Example:
There is a test.txt file in the current directory, but no b.txt file.

[wqf@b1i10 ~]$ ls test.txt
test.txt
[wqf@b1i10 ~]$ ls b.txt
ls: cannot access b.txt: No such file or directory

Output the normal content to the file file.out, and output the error content to the file file.err.

[wqf@b1i10 ~]$ ls test.txt b.txt 1>file.out 2>file.err
[wqf@b1i10 ~]$ cat file.out
test.txt
[wqf@b1i10 ~]$ cat file.err
ls: cannot access b.txt: No such file or directory

3. The meaning of various redirections

1. The meaning of 2>&1

Meaning: Redirect standard error output to standard output
The symbol >& is a redirection symbol, indicating that standard error output is added to standard output.
It can be understood:
1. Originally 1 (standard output) is output to the screen
2, execute >log, output 1 (standard output) to the log file
3, execute 2>&1, add 2 (standard error output) to 1 (standard output ), because 1 (standard output) is output to the log file, so 2 (standard error output) also points to the log.

For example:

nohup sh /root/model_name/script/black_table_stat.sh > /root/model_name/log/black_table_stat.log 2>&1 &

View black_table_stat.log will find that there are both normal output content and error output content.

2. The meaning of &>file

Redirect both 1 (standard output) and 2 (standard error output) to the file file, which has the same meaning as **>log 2>&1**.

For example:

nohup sh /root/model_name/script/black_table_stat.sh > /root/model_name/log/black_table_stat_1_$(date +"%Y%m%d%H").log 2>&1 &

## 可以简写为以下:
nohup sh /root/model_name/script/black_table_stat.sh &> /root/model_name/log/black_table_stat_1_$(date +"%Y%m%d%H").log &

There is no difference in semantics, generally use the first type, generally use the first type ">log 2>&1"

3. The meaning of 1>&2

Meaning: Redirect standard output to standard error output
The symbol >& is a redirection symbol, which means that standard error output is added to standard output.
It can be understood as:
1. Originally 1 (standard output) is output to the screen.
2. Execute >log to output 1 (standard output) to the log file.
3. Execute 1>&2, and add 1 (standard output) to 2 (standard error output), because 2 (standard error output) still points to the screen.

When we type the command error screen in the terminal, an error will be reported, and the place where the error report is displayed on the screen can be regarded as the standard error output channel 2.
For example:

[wqf@b1i10 ~]$ sh /apps/summary_fz_province/test.sh > /apps/summary_fz_province/test.log 1>&2
$*=
$@=
print each param from 
print each param from 

Run the result, check that there is nothing in the test.log, and the screen will output the normal content of running test.sh.

4. The meaning of /dev/null

/dev/null is a special file that writes to it are discarded; if you try to read from it, nothing will be read.

If you want to block standard output and standard error output, you can write like this:

command > /dev/null 2>&1

It can be understood as:
1. Originally 1 (standard output) is output to the screen
2. Execute > /dev/null, output 1 (standard output) to the /dev/null file
3. Execute 2>&1, and output 2 (standard error ) to 1 (standard output), because 1 (standard output) is output to the /dev/null file, so 2 (standard error output) also points to /dev/null.

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/130349575