What is the difference between shell Learning (and?
Strictly speaking, <a FD must be specified before the symbol (there cannot be blank space between them ),
Because 0 is the default value of <, <and 0 <are the same!
> This method is used to export data to a file. If the file does not exist, the file is created. If the file exists, the file is cleared. Generally, this method is used when we back up and clear the log file: back up the log first, and then use '>' to clear the log file (the file size is changed to 0 bytes);> This is to append the output content to the target file. If the file does not exist, create the file. If the file exists, append the new content to the end of the file, and the original content in the file will not be affected.
After you understand that 0 <originally changed the data input channel of stdin, I believe you need to understand the following two redirection
It's not hard:
* 1>
* 2>
The former is to change the data output channel of stdout, and the latter is to change the data output channel of stderr.
Both switch the data originally sent to the monitor to the output to the specified file.
Because 1 is the default value of>, 1> is the same as>, and stdout is changed.
From the file system perspective, a single file can only be written to a single FD for a single time.
If both stdout (1) and stderr (2) are writing file. both,
It depends on whether they encounter simultaneous competition when writing data. It is basically the principle of "first win first.
2> & 1 means to output stderr in parallel with stdout.
* 1> & 2 or> & 2 means to output stdout with stderr
In the Linux File System, a device is located at/dev/null.
This null can be useful in I/O Redirection:
* If you convert FD1 and FD2 to/dev/null, you can remove stdout and stderr.
* If FD0 is connected to/dev/null, it is read into nothing.
For example, when we execute a program, the screen will simultaneously send stdout and stderr,
If you do not want to see stderr (or save it to an archive), you can:
Code:
$ Ls my. file no. such. file 2>/dev/null
My. file
To the contrary: Just Want To See stderr? Not easy yet! Just get stdout to null.
To prevent redirection from flushing the original file content, you can choose> append to the end of the file.
However, once you use> Redirection again, all the content is washed away.
Then, you can use the following command to set whether to allow redirection to overwrite existing files.
Set-o noclobber/* set to prohibit redirect overwrite */
Set + o noclobber/* set to allow redirect overwrite */
After $ cat <file> file, the file results of the original content are washed away!
It is not difficult to understand this concept. This is just a matter of priority:
* In IO Redirection, The stdout and stderr pipelines are prepared before reading data from stdin.
That is to say, in the above example,> file first clears the file and then reads it into <file,
But at this time the file has been cleared, so it becomes unable to read any information...
In the if else formula, else does not need to be used, but then is required.
(If you do not want to run any command after then, the ":" null command can be used instead ).