I/O redirect Detailed and application example
1, the basic concept (this is to understand the knowledge behind the premise, please be sure to understand)
A, I/O redirection is usually related to FD, Shell FD is usually 10, namely 0~9;
b, commonly used FD has 3, for 0 (stdin, standard input), 1 (stdout, standard output), 2 (stderr, standard error Output), the default and keyboard, monitor, monitor related;
C, the use of < to change the read Data channel (stdin), from the designated file read;
D, use > To change the sent out data channel (stdout, stderr), so that the output to the designated file;
E, 0 is the default value of <, so < is the same as 0<;,> is the same as 1>;
F, in IO Redirect, stdout and stderr pipeline will be ready before the data from stdin read;
G, Pipeline "|" (Pipe line): The stdout of the previous command receives the stdin of the next command;
H, tee command is without affecting the original I/O, the stdout copies of a copy to the file;
I, Bash (Ksh) procedure to execute a command: Parse command-variable Evaluation-command substitution (' and $ ())-redirect-wildcard expansion-Determine path-execute command;
J, () put command group Sub-shell to execute, also known as nested Sub-shell, it has a very important feature is: Inherit the parent shell standard input, output, and error plus any Other open file descriptors.
K, EXEC command: Often used to replace the current shell and restart a shell, in other words, did not start the child shell. Any existing environment will be purged when this command is used. When exec operates on a file descriptor, it is only then that EXEC does not overwrite your current shell environment.
2. Basic IO
cmd > file redirects the stdout to the file;
cmd >> file redirect stdout to file (append);
CMD 1> Fiel to redirect stdout to file;
cmd > File 2>&1 redirects stdout and stderr to file files;
cmd 2> file to redirect stderr to file;
cmd 2>> file to redirect stderr to file (append);
cmd >> file 2>&1 redirects stderr and stderr to file files (append);
cmd < file >file2 cmd command with the file file as stdin, to file2 files as stdout;
Cat <>file opens file in read-write mode;
cmd < file cmd command to use file as stdin;
CMD << delimiter here document, read from stdin, until it encounters the delimiter delimiter.