Data Flow Redirection
Redirecting:> and >> is the standard output directed to a file or appended to a file.
Data Flow Redirection
$ Echo ' Hello World ' > redirect
$ Echo ' www.baidu.com ' >> redirect
$ cat redirect
<, <<, >, >> they differ in the direction of redirection inconsistent,> means left-to-right,< represents right-to-left.
Simple redirection
3 Special devices for terminal display and output: stdin (standard input), stdout (standard output), stderr (standard error output)
File Descriptor: The file descriptor is formally a non-negative integer. In fact, he is an index value that points to the record table in which the kernel opens a file for each process maintained by the process. When a program opens an existing file or creates a new file, the kernel returns a file descriptor to the process.
File Descriptor Device File description
0/dev/stdin Standard input
1/dev/stdout Standard Output
2/dev/stderr standard Error
There's also a symbol-. Can be used as a previous command at the same time.
The pipeline defaults to the input of the output of the previous command to the next command, and redirects typically require a file to establish a link to the two commands.
Standard error redirection: Standard output and standard errors are usually pointed to the screen display of the pseudo-terminal.
$ cat documents/test.c\~ hello.c #报错 hello.c not present
$ cat documents/test.c\~ hello.c > File1 #报错 hello.c not present
$ cat documents/test.c\~ hello.c > File1 2>&1 #将标准错误重定向到标准输出, and then the standard output redirection file
$ cat File1 #可查看错误信息
$ cat documents/test.c\~ hello.c &> file2 #将标准错误重定向到标准输出, and then the standard output redirection file
Use the tee command to redirect to multiple files at the same time
$ Echo ' Hello World ' | Tee Hello #将字符输出到hello的同时输出到终端
Permanent redirection: Use the EXEC command for permanent redirection. The role of the EXEC command is to replace the current shell with the specified command and replace the current process with a process, or to specify a new redirect
$ zsh #开启一个子shell
$ exec 1>somefile #使用exec替换当前进程的重定向, redirecting standard output to a file
$ ls
$ exit
$ cat Somefile
Create output file Descriptor: By default there are 9 open file descriptors in the shell.
$ cd/dev/fd/;ls-a1 #打开当前shell进程中的文件描述符
Create a new file descriptor
$ zsh
$ exec 3>somefile
$ cd/dev/fd/;ls-a1;cd-#先进入目录, and then view
$ Echo ' This is test ' >&3
$ cat Somefile
$ exit
Close File descriptor
$ exec 3>&-
$ CD/DEV/FD;LS-A1;CD-
The output of a fully shielded command: In Linux there is a device file that becomes a "black hole", and all data that is imported into it will be swallowed. In a Unix-like system, a/dev/null or empty device is a special device file that is typically used to discard unwanted output streams or as empty files for input streams, which are usually done by redirection. Reading it will immediately get an EOF.
$ cat documents/test.c\~ nefile 1>/dev/null 2>&1
Using Xargs to split a parameter list: Xargs is a common command for UNIX and UNIX-like operating systems. It does this by converting the argument list to small pieces to pass to other commands to avoid the problem of too long a parameter list.
$ cut-d:-f1 </etc/passwd | Sort | Xargs echo #将/etc/passwd file by: After splitting the first field sort, use the echo command to generate a list
$ sudo apt-get install Caca-utils
$ Cacaview <pic_file>
$ cacademo
$ cacafire #带颜色的火焰哦
Data Flow Redirection