shell script> / dev / null 2> & 1 specifically described

Brief:

shell scripts often occur in the back of a cmd command followed by> / dev / null 2> & 1, This paper sort out what SHE L L redirection .

Why do I need to redirect?

shell script in the process of implementation, there will be some printing systems, some standard output, some error output, where the output error does not necessarily mean shell script in question, but some unusual system errors or print output, does not affect system operation of. General shell scripts are compared to more complicated instructions. Here is a problem, call the C shell scripting language program, is generally used to call system, where there is a problem, the process of system implementation, shell script is successfully executed, there is no abnormal signal is interrupted during the execution to see if abnormal signal, system calls the shell will terminate.

Here we need to do is to remove the shell script system called abnormal output, here you need to redirect the output of the shell.

shell redirection

When executing shell commands by default open three files, each file has a corresponding file descriptors to facilitate our use:

Types of File descriptor By default The corresponding file handle position
Standard input (standard input) 0 Get input from the keyboard /proc/self/fd/0
Standard output (standard output) 1 Output to the screen (ie, the console) /proc/self/fd/1
Error output (error output) 2 Output to the screen (ie, the console) /proc/self/fd/2

 

 

 

 

We can look for specific information

root@LEDE:/proc/392/fd# ls -all
dr-x------    2 root     root             0 Jul 23 10:36 .
dr-xr-xr-x    8 root     root             0 Jul 23 09:48 ..
lr-x------    1 root     root            64 Jul 23 10:36 0 -> /dev/ttyS0
l-wx------    1 root     root            64 Jul 23 10:36 1 -> /dev/ttyS0
lrwx------    1 root     root            64 Jul 23 10:36 10 -> /dev/tty
l-wx------    1 root     root            64 Jul 23 10:36 2 -> /dev/ttyS0

/ Dev / ttyS0 serial port corresponding to the terminal is.

Output redirection

Output redirection of use is very simple, basic commands as follows:

command Introduction
command >filename The standard output is redirected to a new file
command 1>filename Ditto
command >>filename The standard output is appended to the file
command 1>>filename Ditto
command 2>filename The standard error is redirected to a new file
command 2>>filename The standard error is appended to the new file

 

 

 

 

 

 

We use> or >> to redirect the output. Left symbol represents the file descriptor, if not, represents 1, which is the standard output on the right, the symbol can be a file, it can be an output device. When using>, it will determine the right file exists or not, if any, on the first delete, and then create a new file, does not exist directly created. But when >> is additionally, it does not delete the original file that already exists.

For example:

root@LEDE:/tmp# touch exist.txt
root@LEDE:/tmp# ls exist.txt notexist.txt 1>out
ls: notexist.txt: No such file or directory
root@LEDE:/tmp# cat out 
exist.txt
root@LEDE:/tmp# ls exist.txt notexist.txt 1>>out
ls: notexist.txt: No such file or directory
root@LEDE:/tmp# cat out 
exist.txt
exist.txt
root@LEDE:/tmp# ls exist.txt notexist.txt 2>err
exist.txt
root@LEDE:/tmp# cat err
ls: notexist.txt: No such file or directory
root@LEDE:/tmp# ls exist.txt notexist.txt >out 2>err
root@LEDE:/tmp# cat out 
exist.txt
root@LEDE:/tmp# cat err 
ls: notexist.txt: No such file or directory

Input redirection

Basic input redirection command:

command Introduction
command <filename Filename to file as standard input
command 0<filename Ditto
command <<delimiter Reads from the standard input, until it encounters a delimiter delimiter

 

 

 

 

We use the <input when doing redirects if the sign did not write the value of the left, then the default is 0.

Please experiment with cat.

/dev/null

Handwritten some "linux shell script Raiders" Description:

/ Dev / null is a special device file that any data received will be discarded. Thus, null the device also commonly called barrels bit (bit bucket) or black holes.

Simple to understand is that the redirection to / dev / null file everything will be discarded.

Because these strings output file descriptors, always show up. If we shell programming, operating a command to return a result, we do not want at this time does not want to print the output to the screen , we can redirect to / dev / null this file, the / dev / null this file is responsible for handling the funeral.

Discard this result can not be considered rude delete error output, this operation is an operation to redirect input and output dropped.

>filename 2>&1

1.2> & 1, the error output is bound to the standard output. Because at this time it is the default standard output, which is output to the screen, so the error output will be output to the screen.

2.> filename, the standard output is redirected to a filename.

>/dev/null 2>&1 VS >/dev/null 2>/dev/null

First command> file 2> file means to stdout standard command and the generated output information to the output stderr errors in file. command> file 2> file written this way, stdout and stderr directly to the file, file is opened twice, and stdout and stderr will cover each other, using a rather write FD1 and FD2 of two file simultaneously to seize pipeline.

The command> file 2> & 1 This command will be sent directly to the file stdout, stderr inherited FD1 pipe, and then was sent to file, this file is only opened file written once, i.e. only on one pipe FD1 including the contents of stdout and stderr.

From the IO efficiency, command> file 2> & 1 command efficiency than the command> file 2> file command high efficiency, so when writing shell scripts, we more time using the command> file written 2> & 1.

END

 

Guess you like

Origin www.cnblogs.com/yeanz/p/11230956.html